PNaCl: Add a pass to convert llvm.global_ctors to __init_array_start/end

This pass converts LLVM's special symbols llvm.global_ctors and
llvm.global_dtors to concrete arrays, __init_array_start/end and
__fini_array_start/end, that are usable by a C library.

The pass sorts the contents of global_ctors/dtors according to the
priority values they contain and removes the priority values.

This pass will allow us to simplify the PNaCl ABI so that less is done
at translation time.

BUG= http://code.google.com/p/nativeclient/issues/detail?id=3018
TEST=test/Transforms/NaCl/expand-ctors*.ll

Review URL: https://codereview.chromium.org/10977030

(cherry picked from commit 7ef7d6db32b8a009c749a4d4f070cc7a72b1f3ff)

Conflicts:
	include/llvm/InitializePasses.h
	lib/Transforms/CMakeLists.txt
	lib/Transforms/LLVMBuild.txt
	lib/Transforms/Makefile
PNaCl: Fix nacl-expand-ctors pass to handle llvm.global_ctors being zeroinitializer

This case can happen if optimisation removes initializers.

BUG=http://code.google.com/p/nativeclient/issues/detail?id=3018
TEST=test/Transforms/NaCl/expand-ctors-zeroinit.ll

Review URL: https://codereview.chromium.org/11190028

(cherry picked from commit 19bfdca1e43752e134bafb3014508f66003b8873)
PNaCl: Add ExpandTls pass for expanding out static TLS variables

This replaces each reference to a TLS variable "foo" with the LLVM IR
equivalent of the expression:
  ((struct tls_template *) __nacl_read_tp())->foo

This pass fills out the global variables __tls_template_start,
__tls_template_end etc. which are used by src/untrusted/nacl/tls.c.
These are the symbols that are otherwise defined by a binutils linker
script.

In order to handle the case of TLS variables that occur inside
ConstantExprs, we have a helper pass, ExpandTlsConstantExpr.

BUG=http://code.google.com/p/nativeclient/issues/detail?id=2837
TEST=test/Transforms/NaCl/expand-tls*.ll

Review URL: https://chromiumcodereview.appspot.com/10896042

(cherry picked from commit ffb0eedef4f034996ce59aac3176482617a8044c)

Conflicts:
	include/llvm/InitializePasses.h
Skeleton for pass-based bitcode ABI verifier

This is the skeleton for a verifier for the portion of the PNaCl bitcode ABI
that can be verified after the bitcode has been read into a Module object.

There is a ModulePass for module-level rules (e.g. GV linkage types) and a
FunctionPass for rules that apply to function bodies (e.g. legal instructions).
They are separated this way to keep the verifier streaming-friendly.
For now, the passes are registered but not hooked up, so they can only be run
manually via opt.

There are 2 bits of actual functionality, just so each pass has something to do:
The ModulePass checks the linkage types of GVs, and the FunctionPass checks
instruction opcodes. For now only the terminator instructions are checked, but
the idea is to add the rest of the allowed instructions to the whitelist,
and possibly call operand checks from the switch statement as well.
For now we just print messagees to stderr, but we will probably want a better
way to plumb the errors in the browser in the future.

R=jvoung@chromium.org,sehr@chromium.org
BUG= https://code.google.com/p/nativeclient/issues/detail?id=2196

Review URL: https://codereview.chromium.org/11986002

(cherry picked from commit 6dead1cbf252b2a84f774c9b37f775062281e701)
Move ABI verifier pass declaration to lib/Analysis and add tests
This fixes the move of the verifier passes from lib/Transforms to
lib/Analysis, and adds tests of the current verifier checks.
No new functionality.

R=jvoung@chromium.org,eliben@chromium.org,mseaborn@chromium.org
BUG= https://code.google.com/p/nativeclient/issues/detail?id=2196

Review URL: https://codereview.chromium.org/12017019

(cherry picked from commit 958cd97d295ba41736615e33c4a1aa641989133e)
Add remaining instructions to ABI verifier whitelist
(the current tests only cover allowed opcodes; no testing yet of types, attributes, etc).

R=jvoung@chromium.org,eliben@chromium.org,mseaborn@chromium.org
BUG= https://code.google.com/p/nativeclient/issues/detail?id=2196
TEST= LLVM regression

Review URL: https://codereview.chromium.org/11896044

(cherry picked from commit 8ccd5689414ade8d6dd50706e64179d6d22cba44)
ABI verifier: Verify types of global variables and initializers.

Loosely based on lib/VMCore/TypeFinder.cpp but I can't use that directly because it's not streaming-friendly.
constexprs in initializers are also supported.

R=jvoung@chromium.org,eliben@chromium.org,mseaborn@chromium.org
BUG= https://code.google.com/p/nativeclient/issues/detail?id=2196
TEST=llvm regression

Review URL: https://codereview.chromium.org/12038079

(cherry picked from commit ceb093296a82aa7125357dab6ddb388a37cd595c)
Use 2>&1 to include stderr in pipe instead of |&
Apparently OSX's bash doesn't support it.

R=eliben@chromium.org,jvoung@chromium.org
BUG=none
TEST=llvm regression

Review URL: https://codereview.chromium.org/12089002

(cherry picked from commit 32cf7c0ab28655e22b5915b8a849451d9b0ff97f)
Store bitcode verifier errors rather than dumping to stderr
This allows properly implementing the analysis pass print() method, and
in the future will allow the error message to be sent somewhere useful

R=jvoung@chromium.org,eliben@chromium.org,mseaborn@chromium.org
BUG= https://code.google.com/p/nativeclient/issues/detail?id=2309

Review URL: https://codereview.chromium.org/12220028

(cherry picked from commit 10614352cb0e3c4ea03de74d5e3337ab2223a018)
This CL supersedes the previous 2 outstanding CLs.
1) Remove the ABIVerifyErrors class since it was just a wrapper with no real functionality. Replace with a simple string ostream for now. If in the future we want to have more intelligent error handling, we can come up with something that does what we actually decide we want.
2) When printing type errors in initializers, just print the Type that was invalid, rather than the whole initializer. This is implemented by having checkTypesInValue return the Type that was invalid, rather than just a boolean.
3) Check the type of each instruction (this is just to make sure it works from the function pass). Do not, however, make the passes dependent as in the previous CL (function passes cannot depend on module passes, but there are no checks for this, and opt does not enforce the dependence, and when llc tries to enforce it, very bad things happen)
2) Style cleanup to match LLVM style.

Sorry for the multiple changes in one CL, hopefully it's still small enough to be reviewable.
BUG=

Review URL: https://codereview.chromium.org/12224109

(cherry picked from commit 421e517aba7ed185ca3aaefcb495a004ae10b72d)
Move ABI type checker to its own file

R=jvoung@chromium.org,eliben@chromium.org
BUG=

Review URL: https://codereview.chromium.org/12302030

(cherry picked from commit 06dd2371667be256a846a68e51d376349f041752)

Conflicts:
	lib/Analysis/NaCl/CheckTypes.h
ABI Verifier: Check function return and argument types, and instruction operands.
This should cover everywhere types can hide, except metadata.

R=jvoung@chromium.org,eliben@chromium.org
BUG= https://code.google.com/p/nativeclient/issues/detail?id=2309

Review URL: https://codereview.chromium.org/12319028

(cherry picked from commit 36749ed09580dfff415e80eb98843e44c123e427)

Conflicts:
	lib/Analysis/NaCl/PNaClABIVerifyFunctions.cpp
ABI Verifier: Check types in metadata nodes
This should be the last source of type references

R=jvoung@chromium.org,eliben@chromium.org
BUG= https://code.google.com/p/nativeclient/issues/detail?id=2309

Review URL: https://codereview.chromium.org/12321032

(cherry picked from commit 30f2f338c415a51b59f02bc0e4c7ccfce947248d)
ABI verifier: Add standalone tool pnacl-abicheck
This CL adds a standalone tool pnacl-abicheck for checking the ABI validity of a bitcode file.
It also adds a flag pnaclabi-verify to llc to enable the same checking in llc.
It also improves the mechanism for handling and reporting validation errors and uses it in both tools.

R=jvoung@chromium.org,mseaborn@chromium.org
BUG= https://code.google.com/p/nativeclient/issues/detail?id=2309

Review URL: https://codereview.chromium.org/12449014

(cherry picked from commit cb0dcd0adf5e43486154d143da30710fee0da301)

Conflicts:
	lib/Analysis/NaCl/PNaClABIVerifyModule.cpp
	tools/CMakeLists.txt
	tools/LLVMBuild.txt
	tools/Makefile
	tools/llc/llc.cpp
Cleanup of ABI Checker
Fix spacing of error output, use OwningPtr everywhere at top level,
fix command line parsing

R=jvoung@chromium.org
BUG=

Review URL: https://codereview.chromium.org/12690007

(cherry picked from commit b3ace3f5ed473e4b19dd1d7f9257218cedd37a96)
Fix LLVMBuild.txt for NaCl analysis passes

R=jvoung@chromium.org
BUG= Sandboxed LLC doesn't build correctly anymore without it

Review URL: https://codereview.chromium.org/12432018

(cherry picked from commit d4b96b281bef71816410d5eceea44eb3998e2dbb)

Conflicts:
	tools/llc/LLVMBuild.txt
	tools/llc/Makefile
PNaCl: Add ExpandConstantExpr pass for converting ConstantExprs to Instructions

Once this pass is enabled, it will simplify the language to reduce the
set of constructs that a PNaCl translator needs to handle as part of a
stable wire format for PNaCl.

BUG=https://code.google.com/p/nativeclient/issues/detail?id=3337
TEST=test/Transforms/NaCl/expand-constantexpr.ll

Review URL: https://codereview.chromium.org/12792011

(cherry picked from commit bd7c1e1fa62acc37cf1bed3c1272703bbffd7242)

Conflicts:
	include/llvm/InitializePasses.h
Switch ABI verifier passes to manual initialization.
This lines them up with the rest of the codebase and will make them work
with statically-linked opt.

R=mseaborn@chromium.org
BUG=none
TEST=existing tests keep working (we don't actually use static opt)

Review URL: https://codereview.chromium.org/12902015

(cherry picked from commit f06c1a6b2bf3772607194f832d0371a7073c1427)

Conflicts:
	include/llvm/InitializePasses.h
Apply after-merge fixes to return to working state.

(cherry picked from commit d41567d2ffd3413600162653c08b2365bd5bcbbf)

In an effort to reduce the surface of changes made while cherry picking the PNaCl
transformation passes, I, Richard Diamond, removed all changes to all files except
lib/Transforms/NaCl/ExpandTls.cpp.

Conflicts:
	lib/MC/MCObjectStreamer.cpp
	lib/Target/ARM/ARMAsmPrinter.cpp
	lib/Target/ARM/ARMSubtarget.cpp
	lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
	lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp
	lib/Target/Mips/MipsAsmPrinter.cpp
	lib/Target/Mips/MipsDelaySlotFiller.cpp
	lib/Target/Mips/MipsISelDAGToDAG.cpp
	lib/Target/Mips/MipsInstrFPU.td
	lib/Target/Mips/MipsNaClRewritePass.cpp
	lib/Target/X86/MCTargetDesc/X86MCAsmInfo.cpp
	lib/Target/X86/X86FastISel.cpp
	lib/Target/X86/X86ISelLowering.cpp
	lib/Target/X86/X86TargetMachine.cpp
	lib/Transforms/NaCl/ExpandTls.cpp
	test/NaCl/ARM/neon-vld1-sandboxing.ll
	test/NaCl/ARM/neon-vld2-sandboxing.ll
	test/NaCl/ARM/neon-vld3-sandboxing.ll
	test/NaCl/ARM/neon-vld4-sandboxing.ll
	test/NaCl/ARM/neon-vlddup-sandboxing.ll
	test/NaCl/ARM/neon-vldlane-sandboxing.ll
	test/NaCl/ARM/neon-vst1-sandboxing.ll
	test/NaCl/ARM/neon-vst2-sandboxing.ll
	test/NaCl/ARM/neon-vst3-sandboxing.ll
	test/NaCl/ARM/neon-vst4-sandboxing.ll
	test/NaCl/ARM/neon-vstlane-sandboxing.ll
PNaCl: Add ExpandVarArgs pass for expanding out variable-args function calls

Once this pass is enabled, it will simplify the language to reduce the
set of constructs that a PNaCl translator needs to handle as part of a
stable wire format for PNaCl.

BUG=https://code.google.com/p/nativeclient/issues/detail?id=3338
TEST=test/Transforms/NaCl/expand-varargs.ll

Review URL: https://codereview.chromium.org/12481021

(cherry picked from commit 7216560b2f1e66a7c9bb9a1b344ae6e9f0f89b7b)