1*ef860a24SChandler Carruth //===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
2*ef860a24SChandler Carruth //
3*ef860a24SChandler Carruth //                     The LLVM Compiler Infrastructure
4*ef860a24SChandler Carruth //
5*ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source
6*ef860a24SChandler Carruth // License. See LICENSE.TXT for details.
7*ef860a24SChandler Carruth //
8*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
9*ef860a24SChandler Carruth //
10*ef860a24SChandler Carruth // This file implements all of the non-inline methods for the LLVM instruction
11*ef860a24SChandler Carruth // classes.
12*ef860a24SChandler Carruth //
13*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
14*ef860a24SChandler Carruth 
15*ef860a24SChandler Carruth #include "llvm/Instructions.h"
16*ef860a24SChandler Carruth #include "LLVMContextImpl.h"
17*ef860a24SChandler Carruth #include "llvm/Constants.h"
18*ef860a24SChandler Carruth #include "llvm/DataLayout.h"
19*ef860a24SChandler Carruth #include "llvm/DerivedTypes.h"
20*ef860a24SChandler Carruth #include "llvm/Function.h"
21*ef860a24SChandler Carruth #include "llvm/Module.h"
22*ef860a24SChandler Carruth #include "llvm/Operator.h"
23*ef860a24SChandler Carruth #include "llvm/Support/CallSite.h"
24*ef860a24SChandler Carruth #include "llvm/Support/ConstantRange.h"
25*ef860a24SChandler Carruth #include "llvm/Support/ErrorHandling.h"
26*ef860a24SChandler Carruth #include "llvm/Support/MathExtras.h"
27*ef860a24SChandler Carruth using namespace llvm;
28*ef860a24SChandler Carruth 
29*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
30*ef860a24SChandler Carruth //                            CallSite Class
31*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
32*ef860a24SChandler Carruth 
33*ef860a24SChandler Carruth User::op_iterator CallSite::getCallee() const {
34*ef860a24SChandler Carruth   Instruction *II(getInstruction());
35*ef860a24SChandler Carruth   return isCall()
36*ef860a24SChandler Carruth     ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
37*ef860a24SChandler Carruth     : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
38*ef860a24SChandler Carruth }
39*ef860a24SChandler Carruth 
40*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
41*ef860a24SChandler Carruth //                            TerminatorInst Class
42*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
43*ef860a24SChandler Carruth 
44*ef860a24SChandler Carruth // Out of line virtual method, so the vtable, etc has a home.
45*ef860a24SChandler Carruth TerminatorInst::~TerminatorInst() {
46*ef860a24SChandler Carruth }
47*ef860a24SChandler Carruth 
48*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
49*ef860a24SChandler Carruth //                           UnaryInstruction Class
50*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
51*ef860a24SChandler Carruth 
52*ef860a24SChandler Carruth // Out of line virtual method, so the vtable, etc has a home.
53*ef860a24SChandler Carruth UnaryInstruction::~UnaryInstruction() {
54*ef860a24SChandler Carruth }
55*ef860a24SChandler Carruth 
56*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
57*ef860a24SChandler Carruth //                              SelectInst Class
58*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
59*ef860a24SChandler Carruth 
60*ef860a24SChandler Carruth /// areInvalidOperands - Return a string if the specified operands are invalid
61*ef860a24SChandler Carruth /// for a select operation, otherwise return null.
62*ef860a24SChandler Carruth const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63*ef860a24SChandler Carruth   if (Op1->getType() != Op2->getType())
64*ef860a24SChandler Carruth     return "both values to select must have same type";
65*ef860a24SChandler Carruth 
66*ef860a24SChandler Carruth   if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
67*ef860a24SChandler Carruth     // Vector select.
68*ef860a24SChandler Carruth     if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
69*ef860a24SChandler Carruth       return "vector select condition element type must be i1";
70*ef860a24SChandler Carruth     VectorType *ET = dyn_cast<VectorType>(Op1->getType());
71*ef860a24SChandler Carruth     if (ET == 0)
72*ef860a24SChandler Carruth       return "selected values for vector select must be vectors";
73*ef860a24SChandler Carruth     if (ET->getNumElements() != VT->getNumElements())
74*ef860a24SChandler Carruth       return "vector select requires selected vectors to have "
75*ef860a24SChandler Carruth                    "the same vector length as select condition";
76*ef860a24SChandler Carruth   } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
77*ef860a24SChandler Carruth     return "select condition must be i1 or <n x i1>";
78*ef860a24SChandler Carruth   }
79*ef860a24SChandler Carruth   return 0;
80*ef860a24SChandler Carruth }
81*ef860a24SChandler Carruth 
82*ef860a24SChandler Carruth 
83*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
84*ef860a24SChandler Carruth //                               PHINode Class
85*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
86*ef860a24SChandler Carruth 
87*ef860a24SChandler Carruth PHINode::PHINode(const PHINode &PN)
88*ef860a24SChandler Carruth   : Instruction(PN.getType(), Instruction::PHI,
89*ef860a24SChandler Carruth                 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
90*ef860a24SChandler Carruth     ReservedSpace(PN.getNumOperands()) {
91*ef860a24SChandler Carruth   std::copy(PN.op_begin(), PN.op_end(), op_begin());
92*ef860a24SChandler Carruth   std::copy(PN.block_begin(), PN.block_end(), block_begin());
93*ef860a24SChandler Carruth   SubclassOptionalData = PN.SubclassOptionalData;
94*ef860a24SChandler Carruth }
95*ef860a24SChandler Carruth 
96*ef860a24SChandler Carruth PHINode::~PHINode() {
97*ef860a24SChandler Carruth   dropHungoffUses();
98*ef860a24SChandler Carruth }
99*ef860a24SChandler Carruth 
100*ef860a24SChandler Carruth Use *PHINode::allocHungoffUses(unsigned N) const {
101*ef860a24SChandler Carruth   // Allocate the array of Uses of the incoming values, followed by a pointer
102*ef860a24SChandler Carruth   // (with bottom bit set) to the User, followed by the array of pointers to
103*ef860a24SChandler Carruth   // the incoming basic blocks.
104*ef860a24SChandler Carruth   size_t size = N * sizeof(Use) + sizeof(Use::UserRef)
105*ef860a24SChandler Carruth     + N * sizeof(BasicBlock*);
106*ef860a24SChandler Carruth   Use *Begin = static_cast<Use*>(::operator new(size));
107*ef860a24SChandler Carruth   Use *End = Begin + N;
108*ef860a24SChandler Carruth   (void) new(End) Use::UserRef(const_cast<PHINode*>(this), 1);
109*ef860a24SChandler Carruth   return Use::initTags(Begin, End);
110*ef860a24SChandler Carruth }
111*ef860a24SChandler Carruth 
112*ef860a24SChandler Carruth // removeIncomingValue - Remove an incoming value.  This is useful if a
113*ef860a24SChandler Carruth // predecessor basic block is deleted.
114*ef860a24SChandler Carruth Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
115*ef860a24SChandler Carruth   Value *Removed = getIncomingValue(Idx);
116*ef860a24SChandler Carruth 
117*ef860a24SChandler Carruth   // Move everything after this operand down.
118*ef860a24SChandler Carruth   //
119*ef860a24SChandler Carruth   // FIXME: we could just swap with the end of the list, then erase.  However,
120*ef860a24SChandler Carruth   // clients might not expect this to happen.  The code as it is thrashes the
121*ef860a24SChandler Carruth   // use/def lists, which is kinda lame.
122*ef860a24SChandler Carruth   std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
123*ef860a24SChandler Carruth   std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
124*ef860a24SChandler Carruth 
125*ef860a24SChandler Carruth   // Nuke the last value.
126*ef860a24SChandler Carruth   Op<-1>().set(0);
127*ef860a24SChandler Carruth   --NumOperands;
128*ef860a24SChandler Carruth 
129*ef860a24SChandler Carruth   // If the PHI node is dead, because it has zero entries, nuke it now.
130*ef860a24SChandler Carruth   if (getNumOperands() == 0 && DeletePHIIfEmpty) {
131*ef860a24SChandler Carruth     // If anyone is using this PHI, make them use a dummy value instead...
132*ef860a24SChandler Carruth     replaceAllUsesWith(UndefValue::get(getType()));
133*ef860a24SChandler Carruth     eraseFromParent();
134*ef860a24SChandler Carruth   }
135*ef860a24SChandler Carruth   return Removed;
136*ef860a24SChandler Carruth }
137*ef860a24SChandler Carruth 
138*ef860a24SChandler Carruth /// growOperands - grow operands - This grows the operand list in response
139*ef860a24SChandler Carruth /// to a push_back style of operation.  This grows the number of ops by 1.5
140*ef860a24SChandler Carruth /// times.
141*ef860a24SChandler Carruth ///
142*ef860a24SChandler Carruth void PHINode::growOperands() {
143*ef860a24SChandler Carruth   unsigned e = getNumOperands();
144*ef860a24SChandler Carruth   unsigned NumOps = e + e / 2;
145*ef860a24SChandler Carruth   if (NumOps < 2) NumOps = 2;      // 2 op PHI nodes are VERY common.
146*ef860a24SChandler Carruth 
147*ef860a24SChandler Carruth   Use *OldOps = op_begin();
148*ef860a24SChandler Carruth   BasicBlock **OldBlocks = block_begin();
149*ef860a24SChandler Carruth 
150*ef860a24SChandler Carruth   ReservedSpace = NumOps;
151*ef860a24SChandler Carruth   OperandList = allocHungoffUses(ReservedSpace);
152*ef860a24SChandler Carruth 
153*ef860a24SChandler Carruth   std::copy(OldOps, OldOps + e, op_begin());
154*ef860a24SChandler Carruth   std::copy(OldBlocks, OldBlocks + e, block_begin());
155*ef860a24SChandler Carruth 
156*ef860a24SChandler Carruth   Use::zap(OldOps, OldOps + e, true);
157*ef860a24SChandler Carruth }
158*ef860a24SChandler Carruth 
159*ef860a24SChandler Carruth /// hasConstantValue - If the specified PHI node always merges together the same
160*ef860a24SChandler Carruth /// value, return the value, otherwise return null.
161*ef860a24SChandler Carruth Value *PHINode::hasConstantValue() const {
162*ef860a24SChandler Carruth   // Exploit the fact that phi nodes always have at least one entry.
163*ef860a24SChandler Carruth   Value *ConstantValue = getIncomingValue(0);
164*ef860a24SChandler Carruth   for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
165*ef860a24SChandler Carruth     if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
166*ef860a24SChandler Carruth       if (ConstantValue != this)
167*ef860a24SChandler Carruth         return 0; // Incoming values not all the same.
168*ef860a24SChandler Carruth        // The case where the first value is this PHI.
169*ef860a24SChandler Carruth       ConstantValue = getIncomingValue(i);
170*ef860a24SChandler Carruth     }
171*ef860a24SChandler Carruth   if (ConstantValue == this)
172*ef860a24SChandler Carruth     return UndefValue::get(getType());
173*ef860a24SChandler Carruth   return ConstantValue;
174*ef860a24SChandler Carruth }
175*ef860a24SChandler Carruth 
176*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
177*ef860a24SChandler Carruth //                       LandingPadInst Implementation
178*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
179*ef860a24SChandler Carruth 
180*ef860a24SChandler Carruth LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
181*ef860a24SChandler Carruth                                unsigned NumReservedValues, const Twine &NameStr,
182*ef860a24SChandler Carruth                                Instruction *InsertBefore)
183*ef860a24SChandler Carruth   : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) {
184*ef860a24SChandler Carruth   init(PersonalityFn, 1 + NumReservedValues, NameStr);
185*ef860a24SChandler Carruth }
186*ef860a24SChandler Carruth 
187*ef860a24SChandler Carruth LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
188*ef860a24SChandler Carruth                                unsigned NumReservedValues, const Twine &NameStr,
189*ef860a24SChandler Carruth                                BasicBlock *InsertAtEnd)
190*ef860a24SChandler Carruth   : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) {
191*ef860a24SChandler Carruth   init(PersonalityFn, 1 + NumReservedValues, NameStr);
192*ef860a24SChandler Carruth }
193*ef860a24SChandler Carruth 
194*ef860a24SChandler Carruth LandingPadInst::LandingPadInst(const LandingPadInst &LP)
195*ef860a24SChandler Carruth   : Instruction(LP.getType(), Instruction::LandingPad,
196*ef860a24SChandler Carruth                 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
197*ef860a24SChandler Carruth     ReservedSpace(LP.getNumOperands()) {
198*ef860a24SChandler Carruth   Use *OL = OperandList, *InOL = LP.OperandList;
199*ef860a24SChandler Carruth   for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
200*ef860a24SChandler Carruth     OL[I] = InOL[I];
201*ef860a24SChandler Carruth 
202*ef860a24SChandler Carruth   setCleanup(LP.isCleanup());
203*ef860a24SChandler Carruth }
204*ef860a24SChandler Carruth 
205*ef860a24SChandler Carruth LandingPadInst::~LandingPadInst() {
206*ef860a24SChandler Carruth   dropHungoffUses();
207*ef860a24SChandler Carruth }
208*ef860a24SChandler Carruth 
209*ef860a24SChandler Carruth LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
210*ef860a24SChandler Carruth                                        unsigned NumReservedClauses,
211*ef860a24SChandler Carruth                                        const Twine &NameStr,
212*ef860a24SChandler Carruth                                        Instruction *InsertBefore) {
213*ef860a24SChandler Carruth   return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
214*ef860a24SChandler Carruth                             InsertBefore);
215*ef860a24SChandler Carruth }
216*ef860a24SChandler Carruth 
217*ef860a24SChandler Carruth LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
218*ef860a24SChandler Carruth                                        unsigned NumReservedClauses,
219*ef860a24SChandler Carruth                                        const Twine &NameStr,
220*ef860a24SChandler Carruth                                        BasicBlock *InsertAtEnd) {
221*ef860a24SChandler Carruth   return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
222*ef860a24SChandler Carruth                             InsertAtEnd);
223*ef860a24SChandler Carruth }
224*ef860a24SChandler Carruth 
225*ef860a24SChandler Carruth void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
226*ef860a24SChandler Carruth                           const Twine &NameStr) {
227*ef860a24SChandler Carruth   ReservedSpace = NumReservedValues;
228*ef860a24SChandler Carruth   NumOperands = 1;
229*ef860a24SChandler Carruth   OperandList = allocHungoffUses(ReservedSpace);
230*ef860a24SChandler Carruth   OperandList[0] = PersFn;
231*ef860a24SChandler Carruth   setName(NameStr);
232*ef860a24SChandler Carruth   setCleanup(false);
233*ef860a24SChandler Carruth }
234*ef860a24SChandler Carruth 
235*ef860a24SChandler Carruth /// growOperands - grow operands - This grows the operand list in response to a
236*ef860a24SChandler Carruth /// push_back style of operation. This grows the number of ops by 2 times.
237*ef860a24SChandler Carruth void LandingPadInst::growOperands(unsigned Size) {
238*ef860a24SChandler Carruth   unsigned e = getNumOperands();
239*ef860a24SChandler Carruth   if (ReservedSpace >= e + Size) return;
240*ef860a24SChandler Carruth   ReservedSpace = (e + Size / 2) * 2;
241*ef860a24SChandler Carruth 
242*ef860a24SChandler Carruth   Use *NewOps = allocHungoffUses(ReservedSpace);
243*ef860a24SChandler Carruth   Use *OldOps = OperandList;
244*ef860a24SChandler Carruth   for (unsigned i = 0; i != e; ++i)
245*ef860a24SChandler Carruth       NewOps[i] = OldOps[i];
246*ef860a24SChandler Carruth 
247*ef860a24SChandler Carruth   OperandList = NewOps;
248*ef860a24SChandler Carruth   Use::zap(OldOps, OldOps + e, true);
249*ef860a24SChandler Carruth }
250*ef860a24SChandler Carruth 
251*ef860a24SChandler Carruth void LandingPadInst::addClause(Value *Val) {
252*ef860a24SChandler Carruth   unsigned OpNo = getNumOperands();
253*ef860a24SChandler Carruth   growOperands(1);
254*ef860a24SChandler Carruth   assert(OpNo < ReservedSpace && "Growing didn't work!");
255*ef860a24SChandler Carruth   ++NumOperands;
256*ef860a24SChandler Carruth   OperandList[OpNo] = Val;
257*ef860a24SChandler Carruth }
258*ef860a24SChandler Carruth 
259*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
260*ef860a24SChandler Carruth //                        CallInst Implementation
261*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
262*ef860a24SChandler Carruth 
263*ef860a24SChandler Carruth CallInst::~CallInst() {
264*ef860a24SChandler Carruth }
265*ef860a24SChandler Carruth 
266*ef860a24SChandler Carruth void CallInst::init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
267*ef860a24SChandler Carruth   assert(NumOperands == Args.size() + 1 && "NumOperands not set up?");
268*ef860a24SChandler Carruth   Op<-1>() = Func;
269*ef860a24SChandler Carruth 
270*ef860a24SChandler Carruth #ifndef NDEBUG
271*ef860a24SChandler Carruth   FunctionType *FTy =
272*ef860a24SChandler Carruth     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
273*ef860a24SChandler Carruth 
274*ef860a24SChandler Carruth   assert((Args.size() == FTy->getNumParams() ||
275*ef860a24SChandler Carruth           (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
276*ef860a24SChandler Carruth          "Calling a function with bad signature!");
277*ef860a24SChandler Carruth 
278*ef860a24SChandler Carruth   for (unsigned i = 0; i != Args.size(); ++i)
279*ef860a24SChandler Carruth     assert((i >= FTy->getNumParams() ||
280*ef860a24SChandler Carruth             FTy->getParamType(i) == Args[i]->getType()) &&
281*ef860a24SChandler Carruth            "Calling a function with a bad signature!");
282*ef860a24SChandler Carruth #endif
283*ef860a24SChandler Carruth 
284*ef860a24SChandler Carruth   std::copy(Args.begin(), Args.end(), op_begin());
285*ef860a24SChandler Carruth   setName(NameStr);
286*ef860a24SChandler Carruth }
287*ef860a24SChandler Carruth 
288*ef860a24SChandler Carruth void CallInst::init(Value *Func, const Twine &NameStr) {
289*ef860a24SChandler Carruth   assert(NumOperands == 1 && "NumOperands not set up?");
290*ef860a24SChandler Carruth   Op<-1>() = Func;
291*ef860a24SChandler Carruth 
292*ef860a24SChandler Carruth #ifndef NDEBUG
293*ef860a24SChandler Carruth   FunctionType *FTy =
294*ef860a24SChandler Carruth     cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
295*ef860a24SChandler Carruth 
296*ef860a24SChandler Carruth   assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
297*ef860a24SChandler Carruth #endif
298*ef860a24SChandler Carruth 
299*ef860a24SChandler Carruth   setName(NameStr);
300*ef860a24SChandler Carruth }
301*ef860a24SChandler Carruth 
302*ef860a24SChandler Carruth CallInst::CallInst(Value *Func, const Twine &Name,
303*ef860a24SChandler Carruth                    Instruction *InsertBefore)
304*ef860a24SChandler Carruth   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
305*ef860a24SChandler Carruth                                    ->getElementType())->getReturnType(),
306*ef860a24SChandler Carruth                 Instruction::Call,
307*ef860a24SChandler Carruth                 OperandTraits<CallInst>::op_end(this) - 1,
308*ef860a24SChandler Carruth                 1, InsertBefore) {
309*ef860a24SChandler Carruth   init(Func, Name);
310*ef860a24SChandler Carruth }
311*ef860a24SChandler Carruth 
312*ef860a24SChandler Carruth CallInst::CallInst(Value *Func, const Twine &Name,
313*ef860a24SChandler Carruth                    BasicBlock *InsertAtEnd)
314*ef860a24SChandler Carruth   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
315*ef860a24SChandler Carruth                                    ->getElementType())->getReturnType(),
316*ef860a24SChandler Carruth                 Instruction::Call,
317*ef860a24SChandler Carruth                 OperandTraits<CallInst>::op_end(this) - 1,
318*ef860a24SChandler Carruth                 1, InsertAtEnd) {
319*ef860a24SChandler Carruth   init(Func, Name);
320*ef860a24SChandler Carruth }
321*ef860a24SChandler Carruth 
322*ef860a24SChandler Carruth CallInst::CallInst(const CallInst &CI)
323*ef860a24SChandler Carruth   : Instruction(CI.getType(), Instruction::Call,
324*ef860a24SChandler Carruth                 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
325*ef860a24SChandler Carruth                 CI.getNumOperands()) {
326*ef860a24SChandler Carruth   setAttributes(CI.getAttributes());
327*ef860a24SChandler Carruth   setTailCall(CI.isTailCall());
328*ef860a24SChandler Carruth   setCallingConv(CI.getCallingConv());
329*ef860a24SChandler Carruth 
330*ef860a24SChandler Carruth   std::copy(CI.op_begin(), CI.op_end(), op_begin());
331*ef860a24SChandler Carruth   SubclassOptionalData = CI.SubclassOptionalData;
332*ef860a24SChandler Carruth }
333*ef860a24SChandler Carruth 
334*ef860a24SChandler Carruth void CallInst::addAttribute(unsigned i, Attribute attr) {
335*ef860a24SChandler Carruth   AttributeSet PAL = getAttributes();
336*ef860a24SChandler Carruth   PAL = PAL.addAttr(getContext(), i, attr);
337*ef860a24SChandler Carruth   setAttributes(PAL);
338*ef860a24SChandler Carruth }
339*ef860a24SChandler Carruth 
340*ef860a24SChandler Carruth void CallInst::removeAttribute(unsigned i, Attribute attr) {
341*ef860a24SChandler Carruth   AttributeSet PAL = getAttributes();
342*ef860a24SChandler Carruth   PAL = PAL.removeAttr(getContext(), i, attr);
343*ef860a24SChandler Carruth   setAttributes(PAL);
344*ef860a24SChandler Carruth }
345*ef860a24SChandler Carruth 
346*ef860a24SChandler Carruth bool CallInst::hasFnAttr(Attribute::AttrKind A) const {
347*ef860a24SChandler Carruth   if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
348*ef860a24SChandler Carruth     return true;
349*ef860a24SChandler Carruth   if (const Function *F = getCalledFunction())
350*ef860a24SChandler Carruth     return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
351*ef860a24SChandler Carruth   return false;
352*ef860a24SChandler Carruth }
353*ef860a24SChandler Carruth 
354*ef860a24SChandler Carruth bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
355*ef860a24SChandler Carruth   if (AttributeList.hasAttribute(i, A))
356*ef860a24SChandler Carruth     return true;
357*ef860a24SChandler Carruth   if (const Function *F = getCalledFunction())
358*ef860a24SChandler Carruth     return F->getAttributes().hasAttribute(i, A);
359*ef860a24SChandler Carruth   return false;
360*ef860a24SChandler Carruth }
361*ef860a24SChandler Carruth 
362*ef860a24SChandler Carruth /// IsConstantOne - Return true only if val is constant int 1
363*ef860a24SChandler Carruth static bool IsConstantOne(Value *val) {
364*ef860a24SChandler Carruth   assert(val && "IsConstantOne does not work with NULL val");
365*ef860a24SChandler Carruth   return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
366*ef860a24SChandler Carruth }
367*ef860a24SChandler Carruth 
368*ef860a24SChandler Carruth static Instruction *createMalloc(Instruction *InsertBefore,
369*ef860a24SChandler Carruth                                  BasicBlock *InsertAtEnd, Type *IntPtrTy,
370*ef860a24SChandler Carruth                                  Type *AllocTy, Value *AllocSize,
371*ef860a24SChandler Carruth                                  Value *ArraySize, Function *MallocF,
372*ef860a24SChandler Carruth                                  const Twine &Name) {
373*ef860a24SChandler Carruth   assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
374*ef860a24SChandler Carruth          "createMalloc needs either InsertBefore or InsertAtEnd");
375*ef860a24SChandler Carruth 
376*ef860a24SChandler Carruth   // malloc(type) becomes:
377*ef860a24SChandler Carruth   //       bitcast (i8* malloc(typeSize)) to type*
378*ef860a24SChandler Carruth   // malloc(type, arraySize) becomes:
379*ef860a24SChandler Carruth   //       bitcast (i8 *malloc(typeSize*arraySize)) to type*
380*ef860a24SChandler Carruth   if (!ArraySize)
381*ef860a24SChandler Carruth     ArraySize = ConstantInt::get(IntPtrTy, 1);
382*ef860a24SChandler Carruth   else if (ArraySize->getType() != IntPtrTy) {
383*ef860a24SChandler Carruth     if (InsertBefore)
384*ef860a24SChandler Carruth       ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
385*ef860a24SChandler Carruth                                               "", InsertBefore);
386*ef860a24SChandler Carruth     else
387*ef860a24SChandler Carruth       ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
388*ef860a24SChandler Carruth                                               "", InsertAtEnd);
389*ef860a24SChandler Carruth   }
390*ef860a24SChandler Carruth 
391*ef860a24SChandler Carruth   if (!IsConstantOne(ArraySize)) {
392*ef860a24SChandler Carruth     if (IsConstantOne(AllocSize)) {
393*ef860a24SChandler Carruth       AllocSize = ArraySize;         // Operand * 1 = Operand
394*ef860a24SChandler Carruth     } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
395*ef860a24SChandler Carruth       Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
396*ef860a24SChandler Carruth                                                      false /*ZExt*/);
397*ef860a24SChandler Carruth       // Malloc arg is constant product of type size and array size
398*ef860a24SChandler Carruth       AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
399*ef860a24SChandler Carruth     } else {
400*ef860a24SChandler Carruth       // Multiply type size by the array size...
401*ef860a24SChandler Carruth       if (InsertBefore)
402*ef860a24SChandler Carruth         AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
403*ef860a24SChandler Carruth                                               "mallocsize", InsertBefore);
404*ef860a24SChandler Carruth       else
405*ef860a24SChandler Carruth         AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
406*ef860a24SChandler Carruth                                               "mallocsize", InsertAtEnd);
407*ef860a24SChandler Carruth     }
408*ef860a24SChandler Carruth   }
409*ef860a24SChandler Carruth 
410*ef860a24SChandler Carruth   assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
411*ef860a24SChandler Carruth   // Create the call to Malloc.
412*ef860a24SChandler Carruth   BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
413*ef860a24SChandler Carruth   Module* M = BB->getParent()->getParent();
414*ef860a24SChandler Carruth   Type *BPTy = Type::getInt8PtrTy(BB->getContext());
415*ef860a24SChandler Carruth   Value *MallocFunc = MallocF;
416*ef860a24SChandler Carruth   if (!MallocFunc)
417*ef860a24SChandler Carruth     // prototype malloc as "void *malloc(size_t)"
418*ef860a24SChandler Carruth     MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
419*ef860a24SChandler Carruth   PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
420*ef860a24SChandler Carruth   CallInst *MCall = NULL;
421*ef860a24SChandler Carruth   Instruction *Result = NULL;
422*ef860a24SChandler Carruth   if (InsertBefore) {
423*ef860a24SChandler Carruth     MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
424*ef860a24SChandler Carruth     Result = MCall;
425*ef860a24SChandler Carruth     if (Result->getType() != AllocPtrType)
426*ef860a24SChandler Carruth       // Create a cast instruction to convert to the right type...
427*ef860a24SChandler Carruth       Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
428*ef860a24SChandler Carruth   } else {
429*ef860a24SChandler Carruth     MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
430*ef860a24SChandler Carruth     Result = MCall;
431*ef860a24SChandler Carruth     if (Result->getType() != AllocPtrType) {
432*ef860a24SChandler Carruth       InsertAtEnd->getInstList().push_back(MCall);
433*ef860a24SChandler Carruth       // Create a cast instruction to convert to the right type...
434*ef860a24SChandler Carruth       Result = new BitCastInst(MCall, AllocPtrType, Name);
435*ef860a24SChandler Carruth     }
436*ef860a24SChandler Carruth   }
437*ef860a24SChandler Carruth   MCall->setTailCall();
438*ef860a24SChandler Carruth   if (Function *F = dyn_cast<Function>(MallocFunc)) {
439*ef860a24SChandler Carruth     MCall->setCallingConv(F->getCallingConv());
440*ef860a24SChandler Carruth     if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
441*ef860a24SChandler Carruth   }
442*ef860a24SChandler Carruth   assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
443*ef860a24SChandler Carruth 
444*ef860a24SChandler Carruth   return Result;
445*ef860a24SChandler Carruth }
446*ef860a24SChandler Carruth 
447*ef860a24SChandler Carruth /// CreateMalloc - Generate the IR for a call to malloc:
448*ef860a24SChandler Carruth /// 1. Compute the malloc call's argument as the specified type's size,
449*ef860a24SChandler Carruth ///    possibly multiplied by the array size if the array size is not
450*ef860a24SChandler Carruth ///    constant 1.
451*ef860a24SChandler Carruth /// 2. Call malloc with that argument.
452*ef860a24SChandler Carruth /// 3. Bitcast the result of the malloc call to the specified type.
453*ef860a24SChandler Carruth Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
454*ef860a24SChandler Carruth                                     Type *IntPtrTy, Type *AllocTy,
455*ef860a24SChandler Carruth                                     Value *AllocSize, Value *ArraySize,
456*ef860a24SChandler Carruth                                     Function * MallocF,
457*ef860a24SChandler Carruth                                     const Twine &Name) {
458*ef860a24SChandler Carruth   return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
459*ef860a24SChandler Carruth                       ArraySize, MallocF, Name);
460*ef860a24SChandler Carruth }
461*ef860a24SChandler Carruth 
462*ef860a24SChandler Carruth /// CreateMalloc - Generate the IR for a call to malloc:
463*ef860a24SChandler Carruth /// 1. Compute the malloc call's argument as the specified type's size,
464*ef860a24SChandler Carruth ///    possibly multiplied by the array size if the array size is not
465*ef860a24SChandler Carruth ///    constant 1.
466*ef860a24SChandler Carruth /// 2. Call malloc with that argument.
467*ef860a24SChandler Carruth /// 3. Bitcast the result of the malloc call to the specified type.
468*ef860a24SChandler Carruth /// Note: This function does not add the bitcast to the basic block, that is the
469*ef860a24SChandler Carruth /// responsibility of the caller.
470*ef860a24SChandler Carruth Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
471*ef860a24SChandler Carruth                                     Type *IntPtrTy, Type *AllocTy,
472*ef860a24SChandler Carruth                                     Value *AllocSize, Value *ArraySize,
473*ef860a24SChandler Carruth                                     Function *MallocF, const Twine &Name) {
474*ef860a24SChandler Carruth   return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
475*ef860a24SChandler Carruth                       ArraySize, MallocF, Name);
476*ef860a24SChandler Carruth }
477*ef860a24SChandler Carruth 
478*ef860a24SChandler Carruth static Instruction* createFree(Value* Source, Instruction *InsertBefore,
479*ef860a24SChandler Carruth                                BasicBlock *InsertAtEnd) {
480*ef860a24SChandler Carruth   assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
481*ef860a24SChandler Carruth          "createFree needs either InsertBefore or InsertAtEnd");
482*ef860a24SChandler Carruth   assert(Source->getType()->isPointerTy() &&
483*ef860a24SChandler Carruth          "Can not free something of nonpointer type!");
484*ef860a24SChandler Carruth 
485*ef860a24SChandler Carruth   BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
486*ef860a24SChandler Carruth   Module* M = BB->getParent()->getParent();
487*ef860a24SChandler Carruth 
488*ef860a24SChandler Carruth   Type *VoidTy = Type::getVoidTy(M->getContext());
489*ef860a24SChandler Carruth   Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
490*ef860a24SChandler Carruth   // prototype free as "void free(void*)"
491*ef860a24SChandler Carruth   Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
492*ef860a24SChandler Carruth   CallInst* Result = NULL;
493*ef860a24SChandler Carruth   Value *PtrCast = Source;
494*ef860a24SChandler Carruth   if (InsertBefore) {
495*ef860a24SChandler Carruth     if (Source->getType() != IntPtrTy)
496*ef860a24SChandler Carruth       PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
497*ef860a24SChandler Carruth     Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
498*ef860a24SChandler Carruth   } else {
499*ef860a24SChandler Carruth     if (Source->getType() != IntPtrTy)
500*ef860a24SChandler Carruth       PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
501*ef860a24SChandler Carruth     Result = CallInst::Create(FreeFunc, PtrCast, "");
502*ef860a24SChandler Carruth   }
503*ef860a24SChandler Carruth   Result->setTailCall();
504*ef860a24SChandler Carruth   if (Function *F = dyn_cast<Function>(FreeFunc))
505*ef860a24SChandler Carruth     Result->setCallingConv(F->getCallingConv());
506*ef860a24SChandler Carruth 
507*ef860a24SChandler Carruth   return Result;
508*ef860a24SChandler Carruth }
509*ef860a24SChandler Carruth 
510*ef860a24SChandler Carruth /// CreateFree - Generate the IR for a call to the builtin free function.
511*ef860a24SChandler Carruth Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
512*ef860a24SChandler Carruth   return createFree(Source, InsertBefore, NULL);
513*ef860a24SChandler Carruth }
514*ef860a24SChandler Carruth 
515*ef860a24SChandler Carruth /// CreateFree - Generate the IR for a call to the builtin free function.
516*ef860a24SChandler Carruth /// Note: This function does not add the call to the basic block, that is the
517*ef860a24SChandler Carruth /// responsibility of the caller.
518*ef860a24SChandler Carruth Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
519*ef860a24SChandler Carruth   Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
520*ef860a24SChandler Carruth   assert(FreeCall && "CreateFree did not create a CallInst");
521*ef860a24SChandler Carruth   return FreeCall;
522*ef860a24SChandler Carruth }
523*ef860a24SChandler Carruth 
524*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
525*ef860a24SChandler Carruth //                        InvokeInst Implementation
526*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
527*ef860a24SChandler Carruth 
528*ef860a24SChandler Carruth void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
529*ef860a24SChandler Carruth                       ArrayRef<Value *> Args, const Twine &NameStr) {
530*ef860a24SChandler Carruth   assert(NumOperands == 3 + Args.size() && "NumOperands not set up?");
531*ef860a24SChandler Carruth   Op<-3>() = Fn;
532*ef860a24SChandler Carruth   Op<-2>() = IfNormal;
533*ef860a24SChandler Carruth   Op<-1>() = IfException;
534*ef860a24SChandler Carruth 
535*ef860a24SChandler Carruth #ifndef NDEBUG
536*ef860a24SChandler Carruth   FunctionType *FTy =
537*ef860a24SChandler Carruth     cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
538*ef860a24SChandler Carruth 
539*ef860a24SChandler Carruth   assert(((Args.size() == FTy->getNumParams()) ||
540*ef860a24SChandler Carruth           (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
541*ef860a24SChandler Carruth          "Invoking a function with bad signature");
542*ef860a24SChandler Carruth 
543*ef860a24SChandler Carruth   for (unsigned i = 0, e = Args.size(); i != e; i++)
544*ef860a24SChandler Carruth     assert((i >= FTy->getNumParams() ||
545*ef860a24SChandler Carruth             FTy->getParamType(i) == Args[i]->getType()) &&
546*ef860a24SChandler Carruth            "Invoking a function with a bad signature!");
547*ef860a24SChandler Carruth #endif
548*ef860a24SChandler Carruth 
549*ef860a24SChandler Carruth   std::copy(Args.begin(), Args.end(), op_begin());
550*ef860a24SChandler Carruth   setName(NameStr);
551*ef860a24SChandler Carruth }
552*ef860a24SChandler Carruth 
553*ef860a24SChandler Carruth InvokeInst::InvokeInst(const InvokeInst &II)
554*ef860a24SChandler Carruth   : TerminatorInst(II.getType(), Instruction::Invoke,
555*ef860a24SChandler Carruth                    OperandTraits<InvokeInst>::op_end(this)
556*ef860a24SChandler Carruth                    - II.getNumOperands(),
557*ef860a24SChandler Carruth                    II.getNumOperands()) {
558*ef860a24SChandler Carruth   setAttributes(II.getAttributes());
559*ef860a24SChandler Carruth   setCallingConv(II.getCallingConv());
560*ef860a24SChandler Carruth   std::copy(II.op_begin(), II.op_end(), op_begin());
561*ef860a24SChandler Carruth   SubclassOptionalData = II.SubclassOptionalData;
562*ef860a24SChandler Carruth }
563*ef860a24SChandler Carruth 
564*ef860a24SChandler Carruth BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
565*ef860a24SChandler Carruth   return getSuccessor(idx);
566*ef860a24SChandler Carruth }
567*ef860a24SChandler Carruth unsigned InvokeInst::getNumSuccessorsV() const {
568*ef860a24SChandler Carruth   return getNumSuccessors();
569*ef860a24SChandler Carruth }
570*ef860a24SChandler Carruth void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
571*ef860a24SChandler Carruth   return setSuccessor(idx, B);
572*ef860a24SChandler Carruth }
573*ef860a24SChandler Carruth 
574*ef860a24SChandler Carruth bool InvokeInst::hasFnAttr(Attribute::AttrKind A) const {
575*ef860a24SChandler Carruth   if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
576*ef860a24SChandler Carruth     return true;
577*ef860a24SChandler Carruth   if (const Function *F = getCalledFunction())
578*ef860a24SChandler Carruth     return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
579*ef860a24SChandler Carruth   return false;
580*ef860a24SChandler Carruth }
581*ef860a24SChandler Carruth 
582*ef860a24SChandler Carruth bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
583*ef860a24SChandler Carruth   if (AttributeList.hasAttribute(i, A))
584*ef860a24SChandler Carruth     return true;
585*ef860a24SChandler Carruth   if (const Function *F = getCalledFunction())
586*ef860a24SChandler Carruth     return F->getAttributes().hasAttribute(i, A);
587*ef860a24SChandler Carruth   return false;
588*ef860a24SChandler Carruth }
589*ef860a24SChandler Carruth 
590*ef860a24SChandler Carruth void InvokeInst::addAttribute(unsigned i, Attribute attr) {
591*ef860a24SChandler Carruth   AttributeSet PAL = getAttributes();
592*ef860a24SChandler Carruth   PAL = PAL.addAttr(getContext(), i, attr);
593*ef860a24SChandler Carruth   setAttributes(PAL);
594*ef860a24SChandler Carruth }
595*ef860a24SChandler Carruth 
596*ef860a24SChandler Carruth void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
597*ef860a24SChandler Carruth   AttributeSet PAL = getAttributes();
598*ef860a24SChandler Carruth   PAL = PAL.removeAttr(getContext(), i, attr);
599*ef860a24SChandler Carruth   setAttributes(PAL);
600*ef860a24SChandler Carruth }
601*ef860a24SChandler Carruth 
602*ef860a24SChandler Carruth LandingPadInst *InvokeInst::getLandingPadInst() const {
603*ef860a24SChandler Carruth   return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
604*ef860a24SChandler Carruth }
605*ef860a24SChandler Carruth 
606*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
607*ef860a24SChandler Carruth //                        ReturnInst Implementation
608*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
609*ef860a24SChandler Carruth 
610*ef860a24SChandler Carruth ReturnInst::ReturnInst(const ReturnInst &RI)
611*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
612*ef860a24SChandler Carruth                    OperandTraits<ReturnInst>::op_end(this) -
613*ef860a24SChandler Carruth                      RI.getNumOperands(),
614*ef860a24SChandler Carruth                    RI.getNumOperands()) {
615*ef860a24SChandler Carruth   if (RI.getNumOperands())
616*ef860a24SChandler Carruth     Op<0>() = RI.Op<0>();
617*ef860a24SChandler Carruth   SubclassOptionalData = RI.SubclassOptionalData;
618*ef860a24SChandler Carruth }
619*ef860a24SChandler Carruth 
620*ef860a24SChandler Carruth ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
621*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
622*ef860a24SChandler Carruth                    OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
623*ef860a24SChandler Carruth                    InsertBefore) {
624*ef860a24SChandler Carruth   if (retVal)
625*ef860a24SChandler Carruth     Op<0>() = retVal;
626*ef860a24SChandler Carruth }
627*ef860a24SChandler Carruth ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
628*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
629*ef860a24SChandler Carruth                    OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
630*ef860a24SChandler Carruth                    InsertAtEnd) {
631*ef860a24SChandler Carruth   if (retVal)
632*ef860a24SChandler Carruth     Op<0>() = retVal;
633*ef860a24SChandler Carruth }
634*ef860a24SChandler Carruth ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
635*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
636*ef860a24SChandler Carruth                    OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
637*ef860a24SChandler Carruth }
638*ef860a24SChandler Carruth 
639*ef860a24SChandler Carruth unsigned ReturnInst::getNumSuccessorsV() const {
640*ef860a24SChandler Carruth   return getNumSuccessors();
641*ef860a24SChandler Carruth }
642*ef860a24SChandler Carruth 
643*ef860a24SChandler Carruth /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
644*ef860a24SChandler Carruth /// emit the vtable for the class in this translation unit.
645*ef860a24SChandler Carruth void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
646*ef860a24SChandler Carruth   llvm_unreachable("ReturnInst has no successors!");
647*ef860a24SChandler Carruth }
648*ef860a24SChandler Carruth 
649*ef860a24SChandler Carruth BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
650*ef860a24SChandler Carruth   llvm_unreachable("ReturnInst has no successors!");
651*ef860a24SChandler Carruth }
652*ef860a24SChandler Carruth 
653*ef860a24SChandler Carruth ReturnInst::~ReturnInst() {
654*ef860a24SChandler Carruth }
655*ef860a24SChandler Carruth 
656*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
657*ef860a24SChandler Carruth //                        ResumeInst Implementation
658*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
659*ef860a24SChandler Carruth 
660*ef860a24SChandler Carruth ResumeInst::ResumeInst(const ResumeInst &RI)
661*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
662*ef860a24SChandler Carruth                    OperandTraits<ResumeInst>::op_begin(this), 1) {
663*ef860a24SChandler Carruth   Op<0>() = RI.Op<0>();
664*ef860a24SChandler Carruth }
665*ef860a24SChandler Carruth 
666*ef860a24SChandler Carruth ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
667*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
668*ef860a24SChandler Carruth                    OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
669*ef860a24SChandler Carruth   Op<0>() = Exn;
670*ef860a24SChandler Carruth }
671*ef860a24SChandler Carruth 
672*ef860a24SChandler Carruth ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
673*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
674*ef860a24SChandler Carruth                    OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
675*ef860a24SChandler Carruth   Op<0>() = Exn;
676*ef860a24SChandler Carruth }
677*ef860a24SChandler Carruth 
678*ef860a24SChandler Carruth unsigned ResumeInst::getNumSuccessorsV() const {
679*ef860a24SChandler Carruth   return getNumSuccessors();
680*ef860a24SChandler Carruth }
681*ef860a24SChandler Carruth 
682*ef860a24SChandler Carruth void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
683*ef860a24SChandler Carruth   llvm_unreachable("ResumeInst has no successors!");
684*ef860a24SChandler Carruth }
685*ef860a24SChandler Carruth 
686*ef860a24SChandler Carruth BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
687*ef860a24SChandler Carruth   llvm_unreachable("ResumeInst has no successors!");
688*ef860a24SChandler Carruth }
689*ef860a24SChandler Carruth 
690*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
691*ef860a24SChandler Carruth //                      UnreachableInst Implementation
692*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
693*ef860a24SChandler Carruth 
694*ef860a24SChandler Carruth UnreachableInst::UnreachableInst(LLVMContext &Context,
695*ef860a24SChandler Carruth                                  Instruction *InsertBefore)
696*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
697*ef860a24SChandler Carruth                    0, 0, InsertBefore) {
698*ef860a24SChandler Carruth }
699*ef860a24SChandler Carruth UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
700*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
701*ef860a24SChandler Carruth                    0, 0, InsertAtEnd) {
702*ef860a24SChandler Carruth }
703*ef860a24SChandler Carruth 
704*ef860a24SChandler Carruth unsigned UnreachableInst::getNumSuccessorsV() const {
705*ef860a24SChandler Carruth   return getNumSuccessors();
706*ef860a24SChandler Carruth }
707*ef860a24SChandler Carruth 
708*ef860a24SChandler Carruth void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
709*ef860a24SChandler Carruth   llvm_unreachable("UnreachableInst has no successors!");
710*ef860a24SChandler Carruth }
711*ef860a24SChandler Carruth 
712*ef860a24SChandler Carruth BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
713*ef860a24SChandler Carruth   llvm_unreachable("UnreachableInst has no successors!");
714*ef860a24SChandler Carruth }
715*ef860a24SChandler Carruth 
716*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
717*ef860a24SChandler Carruth //                        BranchInst Implementation
718*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
719*ef860a24SChandler Carruth 
720*ef860a24SChandler Carruth void BranchInst::AssertOK() {
721*ef860a24SChandler Carruth   if (isConditional())
722*ef860a24SChandler Carruth     assert(getCondition()->getType()->isIntegerTy(1) &&
723*ef860a24SChandler Carruth            "May only branch on boolean predicates!");
724*ef860a24SChandler Carruth }
725*ef860a24SChandler Carruth 
726*ef860a24SChandler Carruth BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
727*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
728*ef860a24SChandler Carruth                    OperandTraits<BranchInst>::op_end(this) - 1,
729*ef860a24SChandler Carruth                    1, InsertBefore) {
730*ef860a24SChandler Carruth   assert(IfTrue != 0 && "Branch destination may not be null!");
731*ef860a24SChandler Carruth   Op<-1>() = IfTrue;
732*ef860a24SChandler Carruth }
733*ef860a24SChandler Carruth BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
734*ef860a24SChandler Carruth                        Instruction *InsertBefore)
735*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
736*ef860a24SChandler Carruth                    OperandTraits<BranchInst>::op_end(this) - 3,
737*ef860a24SChandler Carruth                    3, InsertBefore) {
738*ef860a24SChandler Carruth   Op<-1>() = IfTrue;
739*ef860a24SChandler Carruth   Op<-2>() = IfFalse;
740*ef860a24SChandler Carruth   Op<-3>() = Cond;
741*ef860a24SChandler Carruth #ifndef NDEBUG
742*ef860a24SChandler Carruth   AssertOK();
743*ef860a24SChandler Carruth #endif
744*ef860a24SChandler Carruth }
745*ef860a24SChandler Carruth 
746*ef860a24SChandler Carruth BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
747*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
748*ef860a24SChandler Carruth                    OperandTraits<BranchInst>::op_end(this) - 1,
749*ef860a24SChandler Carruth                    1, InsertAtEnd) {
750*ef860a24SChandler Carruth   assert(IfTrue != 0 && "Branch destination may not be null!");
751*ef860a24SChandler Carruth   Op<-1>() = IfTrue;
752*ef860a24SChandler Carruth }
753*ef860a24SChandler Carruth 
754*ef860a24SChandler Carruth BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
755*ef860a24SChandler Carruth            BasicBlock *InsertAtEnd)
756*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
757*ef860a24SChandler Carruth                    OperandTraits<BranchInst>::op_end(this) - 3,
758*ef860a24SChandler Carruth                    3, InsertAtEnd) {
759*ef860a24SChandler Carruth   Op<-1>() = IfTrue;
760*ef860a24SChandler Carruth   Op<-2>() = IfFalse;
761*ef860a24SChandler Carruth   Op<-3>() = Cond;
762*ef860a24SChandler Carruth #ifndef NDEBUG
763*ef860a24SChandler Carruth   AssertOK();
764*ef860a24SChandler Carruth #endif
765*ef860a24SChandler Carruth }
766*ef860a24SChandler Carruth 
767*ef860a24SChandler Carruth 
768*ef860a24SChandler Carruth BranchInst::BranchInst(const BranchInst &BI) :
769*ef860a24SChandler Carruth   TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
770*ef860a24SChandler Carruth                  OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
771*ef860a24SChandler Carruth                  BI.getNumOperands()) {
772*ef860a24SChandler Carruth   Op<-1>() = BI.Op<-1>();
773*ef860a24SChandler Carruth   if (BI.getNumOperands() != 1) {
774*ef860a24SChandler Carruth     assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
775*ef860a24SChandler Carruth     Op<-3>() = BI.Op<-3>();
776*ef860a24SChandler Carruth     Op<-2>() = BI.Op<-2>();
777*ef860a24SChandler Carruth   }
778*ef860a24SChandler Carruth   SubclassOptionalData = BI.SubclassOptionalData;
779*ef860a24SChandler Carruth }
780*ef860a24SChandler Carruth 
781*ef860a24SChandler Carruth void BranchInst::swapSuccessors() {
782*ef860a24SChandler Carruth   assert(isConditional() &&
783*ef860a24SChandler Carruth          "Cannot swap successors of an unconditional branch");
784*ef860a24SChandler Carruth   Op<-1>().swap(Op<-2>());
785*ef860a24SChandler Carruth 
786*ef860a24SChandler Carruth   // Update profile metadata if present and it matches our structural
787*ef860a24SChandler Carruth   // expectations.
788*ef860a24SChandler Carruth   MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
789*ef860a24SChandler Carruth   if (!ProfileData || ProfileData->getNumOperands() != 3)
790*ef860a24SChandler Carruth     return;
791*ef860a24SChandler Carruth 
792*ef860a24SChandler Carruth   // The first operand is the name. Fetch them backwards and build a new one.
793*ef860a24SChandler Carruth   Value *Ops[] = {
794*ef860a24SChandler Carruth     ProfileData->getOperand(0),
795*ef860a24SChandler Carruth     ProfileData->getOperand(2),
796*ef860a24SChandler Carruth     ProfileData->getOperand(1)
797*ef860a24SChandler Carruth   };
798*ef860a24SChandler Carruth   setMetadata(LLVMContext::MD_prof,
799*ef860a24SChandler Carruth               MDNode::get(ProfileData->getContext(), Ops));
800*ef860a24SChandler Carruth }
801*ef860a24SChandler Carruth 
802*ef860a24SChandler Carruth BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
803*ef860a24SChandler Carruth   return getSuccessor(idx);
804*ef860a24SChandler Carruth }
805*ef860a24SChandler Carruth unsigned BranchInst::getNumSuccessorsV() const {
806*ef860a24SChandler Carruth   return getNumSuccessors();
807*ef860a24SChandler Carruth }
808*ef860a24SChandler Carruth void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
809*ef860a24SChandler Carruth   setSuccessor(idx, B);
810*ef860a24SChandler Carruth }
811*ef860a24SChandler Carruth 
812*ef860a24SChandler Carruth 
813*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
814*ef860a24SChandler Carruth //                        AllocaInst Implementation
815*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
816*ef860a24SChandler Carruth 
817*ef860a24SChandler Carruth static Value *getAISize(LLVMContext &Context, Value *Amt) {
818*ef860a24SChandler Carruth   if (!Amt)
819*ef860a24SChandler Carruth     Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
820*ef860a24SChandler Carruth   else {
821*ef860a24SChandler Carruth     assert(!isa<BasicBlock>(Amt) &&
822*ef860a24SChandler Carruth            "Passed basic block into allocation size parameter! Use other ctor");
823*ef860a24SChandler Carruth     assert(Amt->getType()->isIntegerTy() &&
824*ef860a24SChandler Carruth            "Allocation array size is not an integer!");
825*ef860a24SChandler Carruth   }
826*ef860a24SChandler Carruth   return Amt;
827*ef860a24SChandler Carruth }
828*ef860a24SChandler Carruth 
829*ef860a24SChandler Carruth AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
830*ef860a24SChandler Carruth                        const Twine &Name, Instruction *InsertBefore)
831*ef860a24SChandler Carruth   : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
832*ef860a24SChandler Carruth                      getAISize(Ty->getContext(), ArraySize), InsertBefore) {
833*ef860a24SChandler Carruth   setAlignment(0);
834*ef860a24SChandler Carruth   assert(!Ty->isVoidTy() && "Cannot allocate void!");
835*ef860a24SChandler Carruth   setName(Name);
836*ef860a24SChandler Carruth }
837*ef860a24SChandler Carruth 
838*ef860a24SChandler Carruth AllocaInst::AllocaInst(Type *Ty, Value *ArraySize,
839*ef860a24SChandler Carruth                        const Twine &Name, BasicBlock *InsertAtEnd)
840*ef860a24SChandler Carruth   : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
841*ef860a24SChandler Carruth                      getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
842*ef860a24SChandler Carruth   setAlignment(0);
843*ef860a24SChandler Carruth   assert(!Ty->isVoidTy() && "Cannot allocate void!");
844*ef860a24SChandler Carruth   setName(Name);
845*ef860a24SChandler Carruth }
846*ef860a24SChandler Carruth 
847*ef860a24SChandler Carruth AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
848*ef860a24SChandler Carruth                        Instruction *InsertBefore)
849*ef860a24SChandler Carruth   : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
850*ef860a24SChandler Carruth                      getAISize(Ty->getContext(), 0), InsertBefore) {
851*ef860a24SChandler Carruth   setAlignment(0);
852*ef860a24SChandler Carruth   assert(!Ty->isVoidTy() && "Cannot allocate void!");
853*ef860a24SChandler Carruth   setName(Name);
854*ef860a24SChandler Carruth }
855*ef860a24SChandler Carruth 
856*ef860a24SChandler Carruth AllocaInst::AllocaInst(Type *Ty, const Twine &Name,
857*ef860a24SChandler Carruth                        BasicBlock *InsertAtEnd)
858*ef860a24SChandler Carruth   : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
859*ef860a24SChandler Carruth                      getAISize(Ty->getContext(), 0), InsertAtEnd) {
860*ef860a24SChandler Carruth   setAlignment(0);
861*ef860a24SChandler Carruth   assert(!Ty->isVoidTy() && "Cannot allocate void!");
862*ef860a24SChandler Carruth   setName(Name);
863*ef860a24SChandler Carruth }
864*ef860a24SChandler Carruth 
865*ef860a24SChandler Carruth AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
866*ef860a24SChandler Carruth                        const Twine &Name, Instruction *InsertBefore)
867*ef860a24SChandler Carruth   : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
868*ef860a24SChandler Carruth                      getAISize(Ty->getContext(), ArraySize), InsertBefore) {
869*ef860a24SChandler Carruth   setAlignment(Align);
870*ef860a24SChandler Carruth   assert(!Ty->isVoidTy() && "Cannot allocate void!");
871*ef860a24SChandler Carruth   setName(Name);
872*ef860a24SChandler Carruth }
873*ef860a24SChandler Carruth 
874*ef860a24SChandler Carruth AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
875*ef860a24SChandler Carruth                        const Twine &Name, BasicBlock *InsertAtEnd)
876*ef860a24SChandler Carruth   : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
877*ef860a24SChandler Carruth                      getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
878*ef860a24SChandler Carruth   setAlignment(Align);
879*ef860a24SChandler Carruth   assert(!Ty->isVoidTy() && "Cannot allocate void!");
880*ef860a24SChandler Carruth   setName(Name);
881*ef860a24SChandler Carruth }
882*ef860a24SChandler Carruth 
883*ef860a24SChandler Carruth // Out of line virtual method, so the vtable, etc has a home.
884*ef860a24SChandler Carruth AllocaInst::~AllocaInst() {
885*ef860a24SChandler Carruth }
886*ef860a24SChandler Carruth 
887*ef860a24SChandler Carruth void AllocaInst::setAlignment(unsigned Align) {
888*ef860a24SChandler Carruth   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
889*ef860a24SChandler Carruth   assert(Align <= MaximumAlignment &&
890*ef860a24SChandler Carruth          "Alignment is greater than MaximumAlignment!");
891*ef860a24SChandler Carruth   setInstructionSubclassData(Log2_32(Align) + 1);
892*ef860a24SChandler Carruth   assert(getAlignment() == Align && "Alignment representation error!");
893*ef860a24SChandler Carruth }
894*ef860a24SChandler Carruth 
895*ef860a24SChandler Carruth bool AllocaInst::isArrayAllocation() const {
896*ef860a24SChandler Carruth   if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
897*ef860a24SChandler Carruth     return !CI->isOne();
898*ef860a24SChandler Carruth   return true;
899*ef860a24SChandler Carruth }
900*ef860a24SChandler Carruth 
901*ef860a24SChandler Carruth Type *AllocaInst::getAllocatedType() const {
902*ef860a24SChandler Carruth   return getType()->getElementType();
903*ef860a24SChandler Carruth }
904*ef860a24SChandler Carruth 
905*ef860a24SChandler Carruth /// isStaticAlloca - Return true if this alloca is in the entry block of the
906*ef860a24SChandler Carruth /// function and is a constant size.  If so, the code generator will fold it
907*ef860a24SChandler Carruth /// into the prolog/epilog code, so it is basically free.
908*ef860a24SChandler Carruth bool AllocaInst::isStaticAlloca() const {
909*ef860a24SChandler Carruth   // Must be constant size.
910*ef860a24SChandler Carruth   if (!isa<ConstantInt>(getArraySize())) return false;
911*ef860a24SChandler Carruth 
912*ef860a24SChandler Carruth   // Must be in the entry block.
913*ef860a24SChandler Carruth   const BasicBlock *Parent = getParent();
914*ef860a24SChandler Carruth   return Parent == &Parent->getParent()->front();
915*ef860a24SChandler Carruth }
916*ef860a24SChandler Carruth 
917*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
918*ef860a24SChandler Carruth //                           LoadInst Implementation
919*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
920*ef860a24SChandler Carruth 
921*ef860a24SChandler Carruth void LoadInst::AssertOK() {
922*ef860a24SChandler Carruth   assert(getOperand(0)->getType()->isPointerTy() &&
923*ef860a24SChandler Carruth          "Ptr must have pointer type.");
924*ef860a24SChandler Carruth   assert(!(isAtomic() && getAlignment() == 0) &&
925*ef860a24SChandler Carruth          "Alignment required for atomic load");
926*ef860a24SChandler Carruth }
927*ef860a24SChandler Carruth 
928*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
929*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
930*ef860a24SChandler Carruth                      Load, Ptr, InsertBef) {
931*ef860a24SChandler Carruth   setVolatile(false);
932*ef860a24SChandler Carruth   setAlignment(0);
933*ef860a24SChandler Carruth   setAtomic(NotAtomic);
934*ef860a24SChandler Carruth   AssertOK();
935*ef860a24SChandler Carruth   setName(Name);
936*ef860a24SChandler Carruth }
937*ef860a24SChandler Carruth 
938*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
939*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
940*ef860a24SChandler Carruth                      Load, Ptr, InsertAE) {
941*ef860a24SChandler Carruth   setVolatile(false);
942*ef860a24SChandler Carruth   setAlignment(0);
943*ef860a24SChandler Carruth   setAtomic(NotAtomic);
944*ef860a24SChandler Carruth   AssertOK();
945*ef860a24SChandler Carruth   setName(Name);
946*ef860a24SChandler Carruth }
947*ef860a24SChandler Carruth 
948*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
949*ef860a24SChandler Carruth                    Instruction *InsertBef)
950*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
951*ef860a24SChandler Carruth                      Load, Ptr, InsertBef) {
952*ef860a24SChandler Carruth   setVolatile(isVolatile);
953*ef860a24SChandler Carruth   setAlignment(0);
954*ef860a24SChandler Carruth   setAtomic(NotAtomic);
955*ef860a24SChandler Carruth   AssertOK();
956*ef860a24SChandler Carruth   setName(Name);
957*ef860a24SChandler Carruth }
958*ef860a24SChandler Carruth 
959*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
960*ef860a24SChandler Carruth                    BasicBlock *InsertAE)
961*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
962*ef860a24SChandler Carruth                      Load, Ptr, InsertAE) {
963*ef860a24SChandler Carruth   setVolatile(isVolatile);
964*ef860a24SChandler Carruth   setAlignment(0);
965*ef860a24SChandler Carruth   setAtomic(NotAtomic);
966*ef860a24SChandler Carruth   AssertOK();
967*ef860a24SChandler Carruth   setName(Name);
968*ef860a24SChandler Carruth }
969*ef860a24SChandler Carruth 
970*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
971*ef860a24SChandler Carruth                    unsigned Align, Instruction *InsertBef)
972*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
973*ef860a24SChandler Carruth                      Load, Ptr, InsertBef) {
974*ef860a24SChandler Carruth   setVolatile(isVolatile);
975*ef860a24SChandler Carruth   setAlignment(Align);
976*ef860a24SChandler Carruth   setAtomic(NotAtomic);
977*ef860a24SChandler Carruth   AssertOK();
978*ef860a24SChandler Carruth   setName(Name);
979*ef860a24SChandler Carruth }
980*ef860a24SChandler Carruth 
981*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
982*ef860a24SChandler Carruth                    unsigned Align, BasicBlock *InsertAE)
983*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
984*ef860a24SChandler Carruth                      Load, Ptr, InsertAE) {
985*ef860a24SChandler Carruth   setVolatile(isVolatile);
986*ef860a24SChandler Carruth   setAlignment(Align);
987*ef860a24SChandler Carruth   setAtomic(NotAtomic);
988*ef860a24SChandler Carruth   AssertOK();
989*ef860a24SChandler Carruth   setName(Name);
990*ef860a24SChandler Carruth }
991*ef860a24SChandler Carruth 
992*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
993*ef860a24SChandler Carruth                    unsigned Align, AtomicOrdering Order,
994*ef860a24SChandler Carruth                    SynchronizationScope SynchScope,
995*ef860a24SChandler Carruth                    Instruction *InsertBef)
996*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
997*ef860a24SChandler Carruth                      Load, Ptr, InsertBef) {
998*ef860a24SChandler Carruth   setVolatile(isVolatile);
999*ef860a24SChandler Carruth   setAlignment(Align);
1000*ef860a24SChandler Carruth   setAtomic(Order, SynchScope);
1001*ef860a24SChandler Carruth   AssertOK();
1002*ef860a24SChandler Carruth   setName(Name);
1003*ef860a24SChandler Carruth }
1004*ef860a24SChandler Carruth 
1005*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1006*ef860a24SChandler Carruth                    unsigned Align, AtomicOrdering Order,
1007*ef860a24SChandler Carruth                    SynchronizationScope SynchScope,
1008*ef860a24SChandler Carruth                    BasicBlock *InsertAE)
1009*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1010*ef860a24SChandler Carruth                      Load, Ptr, InsertAE) {
1011*ef860a24SChandler Carruth   setVolatile(isVolatile);
1012*ef860a24SChandler Carruth   setAlignment(Align);
1013*ef860a24SChandler Carruth   setAtomic(Order, SynchScope);
1014*ef860a24SChandler Carruth   AssertOK();
1015*ef860a24SChandler Carruth   setName(Name);
1016*ef860a24SChandler Carruth }
1017*ef860a24SChandler Carruth 
1018*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1019*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1020*ef860a24SChandler Carruth                      Load, Ptr, InsertBef) {
1021*ef860a24SChandler Carruth   setVolatile(false);
1022*ef860a24SChandler Carruth   setAlignment(0);
1023*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1024*ef860a24SChandler Carruth   AssertOK();
1025*ef860a24SChandler Carruth   if (Name && Name[0]) setName(Name);
1026*ef860a24SChandler Carruth }
1027*ef860a24SChandler Carruth 
1028*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1029*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1030*ef860a24SChandler Carruth                      Load, Ptr, InsertAE) {
1031*ef860a24SChandler Carruth   setVolatile(false);
1032*ef860a24SChandler Carruth   setAlignment(0);
1033*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1034*ef860a24SChandler Carruth   AssertOK();
1035*ef860a24SChandler Carruth   if (Name && Name[0]) setName(Name);
1036*ef860a24SChandler Carruth }
1037*ef860a24SChandler Carruth 
1038*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1039*ef860a24SChandler Carruth                    Instruction *InsertBef)
1040*ef860a24SChandler Carruth : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1041*ef860a24SChandler Carruth                    Load, Ptr, InsertBef) {
1042*ef860a24SChandler Carruth   setVolatile(isVolatile);
1043*ef860a24SChandler Carruth   setAlignment(0);
1044*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1045*ef860a24SChandler Carruth   AssertOK();
1046*ef860a24SChandler Carruth   if (Name && Name[0]) setName(Name);
1047*ef860a24SChandler Carruth }
1048*ef860a24SChandler Carruth 
1049*ef860a24SChandler Carruth LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1050*ef860a24SChandler Carruth                    BasicBlock *InsertAE)
1051*ef860a24SChandler Carruth   : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1052*ef860a24SChandler Carruth                      Load, Ptr, InsertAE) {
1053*ef860a24SChandler Carruth   setVolatile(isVolatile);
1054*ef860a24SChandler Carruth   setAlignment(0);
1055*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1056*ef860a24SChandler Carruth   AssertOK();
1057*ef860a24SChandler Carruth   if (Name && Name[0]) setName(Name);
1058*ef860a24SChandler Carruth }
1059*ef860a24SChandler Carruth 
1060*ef860a24SChandler Carruth void LoadInst::setAlignment(unsigned Align) {
1061*ef860a24SChandler Carruth   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1062*ef860a24SChandler Carruth   assert(Align <= MaximumAlignment &&
1063*ef860a24SChandler Carruth          "Alignment is greater than MaximumAlignment!");
1064*ef860a24SChandler Carruth   setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
1065*ef860a24SChandler Carruth                              ((Log2_32(Align)+1)<<1));
1066*ef860a24SChandler Carruth   assert(getAlignment() == Align && "Alignment representation error!");
1067*ef860a24SChandler Carruth }
1068*ef860a24SChandler Carruth 
1069*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1070*ef860a24SChandler Carruth //                           StoreInst Implementation
1071*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1072*ef860a24SChandler Carruth 
1073*ef860a24SChandler Carruth void StoreInst::AssertOK() {
1074*ef860a24SChandler Carruth   assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
1075*ef860a24SChandler Carruth   assert(getOperand(1)->getType()->isPointerTy() &&
1076*ef860a24SChandler Carruth          "Ptr must have pointer type!");
1077*ef860a24SChandler Carruth   assert(getOperand(0)->getType() ==
1078*ef860a24SChandler Carruth                  cast<PointerType>(getOperand(1)->getType())->getElementType()
1079*ef860a24SChandler Carruth          && "Ptr must be a pointer to Val type!");
1080*ef860a24SChandler Carruth   assert(!(isAtomic() && getAlignment() == 0) &&
1081*ef860a24SChandler Carruth          "Alignment required for atomic load");
1082*ef860a24SChandler Carruth }
1083*ef860a24SChandler Carruth 
1084*ef860a24SChandler Carruth 
1085*ef860a24SChandler Carruth StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
1086*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(val->getContext()), Store,
1087*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::op_begin(this),
1088*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::operands(this),
1089*ef860a24SChandler Carruth                 InsertBefore) {
1090*ef860a24SChandler Carruth   Op<0>() = val;
1091*ef860a24SChandler Carruth   Op<1>() = addr;
1092*ef860a24SChandler Carruth   setVolatile(false);
1093*ef860a24SChandler Carruth   setAlignment(0);
1094*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1095*ef860a24SChandler Carruth   AssertOK();
1096*ef860a24SChandler Carruth }
1097*ef860a24SChandler Carruth 
1098*ef860a24SChandler Carruth StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
1099*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(val->getContext()), Store,
1100*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::op_begin(this),
1101*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::operands(this),
1102*ef860a24SChandler Carruth                 InsertAtEnd) {
1103*ef860a24SChandler Carruth   Op<0>() = val;
1104*ef860a24SChandler Carruth   Op<1>() = addr;
1105*ef860a24SChandler Carruth   setVolatile(false);
1106*ef860a24SChandler Carruth   setAlignment(0);
1107*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1108*ef860a24SChandler Carruth   AssertOK();
1109*ef860a24SChandler Carruth }
1110*ef860a24SChandler Carruth 
1111*ef860a24SChandler Carruth StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1112*ef860a24SChandler Carruth                      Instruction *InsertBefore)
1113*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(val->getContext()), Store,
1114*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::op_begin(this),
1115*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::operands(this),
1116*ef860a24SChandler Carruth                 InsertBefore) {
1117*ef860a24SChandler Carruth   Op<0>() = val;
1118*ef860a24SChandler Carruth   Op<1>() = addr;
1119*ef860a24SChandler Carruth   setVolatile(isVolatile);
1120*ef860a24SChandler Carruth   setAlignment(0);
1121*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1122*ef860a24SChandler Carruth   AssertOK();
1123*ef860a24SChandler Carruth }
1124*ef860a24SChandler Carruth 
1125*ef860a24SChandler Carruth StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1126*ef860a24SChandler Carruth                      unsigned Align, Instruction *InsertBefore)
1127*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(val->getContext()), Store,
1128*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::op_begin(this),
1129*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::operands(this),
1130*ef860a24SChandler Carruth                 InsertBefore) {
1131*ef860a24SChandler Carruth   Op<0>() = val;
1132*ef860a24SChandler Carruth   Op<1>() = addr;
1133*ef860a24SChandler Carruth   setVolatile(isVolatile);
1134*ef860a24SChandler Carruth   setAlignment(Align);
1135*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1136*ef860a24SChandler Carruth   AssertOK();
1137*ef860a24SChandler Carruth }
1138*ef860a24SChandler Carruth 
1139*ef860a24SChandler Carruth StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1140*ef860a24SChandler Carruth                      unsigned Align, AtomicOrdering Order,
1141*ef860a24SChandler Carruth                      SynchronizationScope SynchScope,
1142*ef860a24SChandler Carruth                      Instruction *InsertBefore)
1143*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(val->getContext()), Store,
1144*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::op_begin(this),
1145*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::operands(this),
1146*ef860a24SChandler Carruth                 InsertBefore) {
1147*ef860a24SChandler Carruth   Op<0>() = val;
1148*ef860a24SChandler Carruth   Op<1>() = addr;
1149*ef860a24SChandler Carruth   setVolatile(isVolatile);
1150*ef860a24SChandler Carruth   setAlignment(Align);
1151*ef860a24SChandler Carruth   setAtomic(Order, SynchScope);
1152*ef860a24SChandler Carruth   AssertOK();
1153*ef860a24SChandler Carruth }
1154*ef860a24SChandler Carruth 
1155*ef860a24SChandler Carruth StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1156*ef860a24SChandler Carruth                      BasicBlock *InsertAtEnd)
1157*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(val->getContext()), Store,
1158*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::op_begin(this),
1159*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::operands(this),
1160*ef860a24SChandler Carruth                 InsertAtEnd) {
1161*ef860a24SChandler Carruth   Op<0>() = val;
1162*ef860a24SChandler Carruth   Op<1>() = addr;
1163*ef860a24SChandler Carruth   setVolatile(isVolatile);
1164*ef860a24SChandler Carruth   setAlignment(0);
1165*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1166*ef860a24SChandler Carruth   AssertOK();
1167*ef860a24SChandler Carruth }
1168*ef860a24SChandler Carruth 
1169*ef860a24SChandler Carruth StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1170*ef860a24SChandler Carruth                      unsigned Align, BasicBlock *InsertAtEnd)
1171*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(val->getContext()), Store,
1172*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::op_begin(this),
1173*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::operands(this),
1174*ef860a24SChandler Carruth                 InsertAtEnd) {
1175*ef860a24SChandler Carruth   Op<0>() = val;
1176*ef860a24SChandler Carruth   Op<1>() = addr;
1177*ef860a24SChandler Carruth   setVolatile(isVolatile);
1178*ef860a24SChandler Carruth   setAlignment(Align);
1179*ef860a24SChandler Carruth   setAtomic(NotAtomic);
1180*ef860a24SChandler Carruth   AssertOK();
1181*ef860a24SChandler Carruth }
1182*ef860a24SChandler Carruth 
1183*ef860a24SChandler Carruth StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1184*ef860a24SChandler Carruth                      unsigned Align, AtomicOrdering Order,
1185*ef860a24SChandler Carruth                      SynchronizationScope SynchScope,
1186*ef860a24SChandler Carruth                      BasicBlock *InsertAtEnd)
1187*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(val->getContext()), Store,
1188*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::op_begin(this),
1189*ef860a24SChandler Carruth                 OperandTraits<StoreInst>::operands(this),
1190*ef860a24SChandler Carruth                 InsertAtEnd) {
1191*ef860a24SChandler Carruth   Op<0>() = val;
1192*ef860a24SChandler Carruth   Op<1>() = addr;
1193*ef860a24SChandler Carruth   setVolatile(isVolatile);
1194*ef860a24SChandler Carruth   setAlignment(Align);
1195*ef860a24SChandler Carruth   setAtomic(Order, SynchScope);
1196*ef860a24SChandler Carruth   AssertOK();
1197*ef860a24SChandler Carruth }
1198*ef860a24SChandler Carruth 
1199*ef860a24SChandler Carruth void StoreInst::setAlignment(unsigned Align) {
1200*ef860a24SChandler Carruth   assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
1201*ef860a24SChandler Carruth   assert(Align <= MaximumAlignment &&
1202*ef860a24SChandler Carruth          "Alignment is greater than MaximumAlignment!");
1203*ef860a24SChandler Carruth   setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
1204*ef860a24SChandler Carruth                              ((Log2_32(Align)+1) << 1));
1205*ef860a24SChandler Carruth   assert(getAlignment() == Align && "Alignment representation error!");
1206*ef860a24SChandler Carruth }
1207*ef860a24SChandler Carruth 
1208*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1209*ef860a24SChandler Carruth //                       AtomicCmpXchgInst Implementation
1210*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1211*ef860a24SChandler Carruth 
1212*ef860a24SChandler Carruth void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
1213*ef860a24SChandler Carruth                              AtomicOrdering Ordering,
1214*ef860a24SChandler Carruth                              SynchronizationScope SynchScope) {
1215*ef860a24SChandler Carruth   Op<0>() = Ptr;
1216*ef860a24SChandler Carruth   Op<1>() = Cmp;
1217*ef860a24SChandler Carruth   Op<2>() = NewVal;
1218*ef860a24SChandler Carruth   setOrdering(Ordering);
1219*ef860a24SChandler Carruth   setSynchScope(SynchScope);
1220*ef860a24SChandler Carruth 
1221*ef860a24SChandler Carruth   assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1222*ef860a24SChandler Carruth          "All operands must be non-null!");
1223*ef860a24SChandler Carruth   assert(getOperand(0)->getType()->isPointerTy() &&
1224*ef860a24SChandler Carruth          "Ptr must have pointer type!");
1225*ef860a24SChandler Carruth   assert(getOperand(1)->getType() ==
1226*ef860a24SChandler Carruth                  cast<PointerType>(getOperand(0)->getType())->getElementType()
1227*ef860a24SChandler Carruth          && "Ptr must be a pointer to Cmp type!");
1228*ef860a24SChandler Carruth   assert(getOperand(2)->getType() ==
1229*ef860a24SChandler Carruth                  cast<PointerType>(getOperand(0)->getType())->getElementType()
1230*ef860a24SChandler Carruth          && "Ptr must be a pointer to NewVal type!");
1231*ef860a24SChandler Carruth   assert(Ordering != NotAtomic &&
1232*ef860a24SChandler Carruth          "AtomicCmpXchg instructions must be atomic!");
1233*ef860a24SChandler Carruth }
1234*ef860a24SChandler Carruth 
1235*ef860a24SChandler Carruth AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1236*ef860a24SChandler Carruth                                      AtomicOrdering Ordering,
1237*ef860a24SChandler Carruth                                      SynchronizationScope SynchScope,
1238*ef860a24SChandler Carruth                                      Instruction *InsertBefore)
1239*ef860a24SChandler Carruth   : Instruction(Cmp->getType(), AtomicCmpXchg,
1240*ef860a24SChandler Carruth                 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1241*ef860a24SChandler Carruth                 OperandTraits<AtomicCmpXchgInst>::operands(this),
1242*ef860a24SChandler Carruth                 InsertBefore) {
1243*ef860a24SChandler Carruth   Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1244*ef860a24SChandler Carruth }
1245*ef860a24SChandler Carruth 
1246*ef860a24SChandler Carruth AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
1247*ef860a24SChandler Carruth                                      AtomicOrdering Ordering,
1248*ef860a24SChandler Carruth                                      SynchronizationScope SynchScope,
1249*ef860a24SChandler Carruth                                      BasicBlock *InsertAtEnd)
1250*ef860a24SChandler Carruth   : Instruction(Cmp->getType(), AtomicCmpXchg,
1251*ef860a24SChandler Carruth                 OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1252*ef860a24SChandler Carruth                 OperandTraits<AtomicCmpXchgInst>::operands(this),
1253*ef860a24SChandler Carruth                 InsertAtEnd) {
1254*ef860a24SChandler Carruth   Init(Ptr, Cmp, NewVal, Ordering, SynchScope);
1255*ef860a24SChandler Carruth }
1256*ef860a24SChandler Carruth 
1257*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1258*ef860a24SChandler Carruth //                       AtomicRMWInst Implementation
1259*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1260*ef860a24SChandler Carruth 
1261*ef860a24SChandler Carruth void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1262*ef860a24SChandler Carruth                          AtomicOrdering Ordering,
1263*ef860a24SChandler Carruth                          SynchronizationScope SynchScope) {
1264*ef860a24SChandler Carruth   Op<0>() = Ptr;
1265*ef860a24SChandler Carruth   Op<1>() = Val;
1266*ef860a24SChandler Carruth   setOperation(Operation);
1267*ef860a24SChandler Carruth   setOrdering(Ordering);
1268*ef860a24SChandler Carruth   setSynchScope(SynchScope);
1269*ef860a24SChandler Carruth 
1270*ef860a24SChandler Carruth   assert(getOperand(0) && getOperand(1) &&
1271*ef860a24SChandler Carruth          "All operands must be non-null!");
1272*ef860a24SChandler Carruth   assert(getOperand(0)->getType()->isPointerTy() &&
1273*ef860a24SChandler Carruth          "Ptr must have pointer type!");
1274*ef860a24SChandler Carruth   assert(getOperand(1)->getType() ==
1275*ef860a24SChandler Carruth          cast<PointerType>(getOperand(0)->getType())->getElementType()
1276*ef860a24SChandler Carruth          && "Ptr must be a pointer to Val type!");
1277*ef860a24SChandler Carruth   assert(Ordering != NotAtomic &&
1278*ef860a24SChandler Carruth          "AtomicRMW instructions must be atomic!");
1279*ef860a24SChandler Carruth }
1280*ef860a24SChandler Carruth 
1281*ef860a24SChandler Carruth AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1282*ef860a24SChandler Carruth                              AtomicOrdering Ordering,
1283*ef860a24SChandler Carruth                              SynchronizationScope SynchScope,
1284*ef860a24SChandler Carruth                              Instruction *InsertBefore)
1285*ef860a24SChandler Carruth   : Instruction(Val->getType(), AtomicRMW,
1286*ef860a24SChandler Carruth                 OperandTraits<AtomicRMWInst>::op_begin(this),
1287*ef860a24SChandler Carruth                 OperandTraits<AtomicRMWInst>::operands(this),
1288*ef860a24SChandler Carruth                 InsertBefore) {
1289*ef860a24SChandler Carruth   Init(Operation, Ptr, Val, Ordering, SynchScope);
1290*ef860a24SChandler Carruth }
1291*ef860a24SChandler Carruth 
1292*ef860a24SChandler Carruth AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1293*ef860a24SChandler Carruth                              AtomicOrdering Ordering,
1294*ef860a24SChandler Carruth                              SynchronizationScope SynchScope,
1295*ef860a24SChandler Carruth                              BasicBlock *InsertAtEnd)
1296*ef860a24SChandler Carruth   : Instruction(Val->getType(), AtomicRMW,
1297*ef860a24SChandler Carruth                 OperandTraits<AtomicRMWInst>::op_begin(this),
1298*ef860a24SChandler Carruth                 OperandTraits<AtomicRMWInst>::operands(this),
1299*ef860a24SChandler Carruth                 InsertAtEnd) {
1300*ef860a24SChandler Carruth   Init(Operation, Ptr, Val, Ordering, SynchScope);
1301*ef860a24SChandler Carruth }
1302*ef860a24SChandler Carruth 
1303*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1304*ef860a24SChandler Carruth //                       FenceInst Implementation
1305*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1306*ef860a24SChandler Carruth 
1307*ef860a24SChandler Carruth FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1308*ef860a24SChandler Carruth                      SynchronizationScope SynchScope,
1309*ef860a24SChandler Carruth                      Instruction *InsertBefore)
1310*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertBefore) {
1311*ef860a24SChandler Carruth   setOrdering(Ordering);
1312*ef860a24SChandler Carruth   setSynchScope(SynchScope);
1313*ef860a24SChandler Carruth }
1314*ef860a24SChandler Carruth 
1315*ef860a24SChandler Carruth FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1316*ef860a24SChandler Carruth                      SynchronizationScope SynchScope,
1317*ef860a24SChandler Carruth                      BasicBlock *InsertAtEnd)
1318*ef860a24SChandler Carruth   : Instruction(Type::getVoidTy(C), Fence, 0, 0, InsertAtEnd) {
1319*ef860a24SChandler Carruth   setOrdering(Ordering);
1320*ef860a24SChandler Carruth   setSynchScope(SynchScope);
1321*ef860a24SChandler Carruth }
1322*ef860a24SChandler Carruth 
1323*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1324*ef860a24SChandler Carruth //                       GetElementPtrInst Implementation
1325*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1326*ef860a24SChandler Carruth 
1327*ef860a24SChandler Carruth void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
1328*ef860a24SChandler Carruth                              const Twine &Name) {
1329*ef860a24SChandler Carruth   assert(NumOperands == 1 + IdxList.size() && "NumOperands not initialized?");
1330*ef860a24SChandler Carruth   OperandList[0] = Ptr;
1331*ef860a24SChandler Carruth   std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
1332*ef860a24SChandler Carruth   setName(Name);
1333*ef860a24SChandler Carruth }
1334*ef860a24SChandler Carruth 
1335*ef860a24SChandler Carruth GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
1336*ef860a24SChandler Carruth   : Instruction(GEPI.getType(), GetElementPtr,
1337*ef860a24SChandler Carruth                 OperandTraits<GetElementPtrInst>::op_end(this)
1338*ef860a24SChandler Carruth                 - GEPI.getNumOperands(),
1339*ef860a24SChandler Carruth                 GEPI.getNumOperands()) {
1340*ef860a24SChandler Carruth   std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
1341*ef860a24SChandler Carruth   SubclassOptionalData = GEPI.SubclassOptionalData;
1342*ef860a24SChandler Carruth }
1343*ef860a24SChandler Carruth 
1344*ef860a24SChandler Carruth /// getIndexedType - Returns the type of the element that would be accessed with
1345*ef860a24SChandler Carruth /// a gep instruction with the specified parameters.
1346*ef860a24SChandler Carruth ///
1347*ef860a24SChandler Carruth /// The Idxs pointer should point to a continuous piece of memory containing the
1348*ef860a24SChandler Carruth /// indices, either as Value* or uint64_t.
1349*ef860a24SChandler Carruth ///
1350*ef860a24SChandler Carruth /// A null type is returned if the indices are invalid for the specified
1351*ef860a24SChandler Carruth /// pointer type.
1352*ef860a24SChandler Carruth ///
1353*ef860a24SChandler Carruth template <typename IndexTy>
1354*ef860a24SChandler Carruth static Type *getIndexedTypeInternal(Type *Ptr, ArrayRef<IndexTy> IdxList) {
1355*ef860a24SChandler Carruth   PointerType *PTy = dyn_cast<PointerType>(Ptr->getScalarType());
1356*ef860a24SChandler Carruth   if (!PTy) return 0;   // Type isn't a pointer type!
1357*ef860a24SChandler Carruth   Type *Agg = PTy->getElementType();
1358*ef860a24SChandler Carruth 
1359*ef860a24SChandler Carruth   // Handle the special case of the empty set index set, which is always valid.
1360*ef860a24SChandler Carruth   if (IdxList.empty())
1361*ef860a24SChandler Carruth     return Agg;
1362*ef860a24SChandler Carruth 
1363*ef860a24SChandler Carruth   // If there is at least one index, the top level type must be sized, otherwise
1364*ef860a24SChandler Carruth   // it cannot be 'stepped over'.
1365*ef860a24SChandler Carruth   if (!Agg->isSized())
1366*ef860a24SChandler Carruth     return 0;
1367*ef860a24SChandler Carruth 
1368*ef860a24SChandler Carruth   unsigned CurIdx = 1;
1369*ef860a24SChandler Carruth   for (; CurIdx != IdxList.size(); ++CurIdx) {
1370*ef860a24SChandler Carruth     CompositeType *CT = dyn_cast<CompositeType>(Agg);
1371*ef860a24SChandler Carruth     if (!CT || CT->isPointerTy()) return 0;
1372*ef860a24SChandler Carruth     IndexTy Index = IdxList[CurIdx];
1373*ef860a24SChandler Carruth     if (!CT->indexValid(Index)) return 0;
1374*ef860a24SChandler Carruth     Agg = CT->getTypeAtIndex(Index);
1375*ef860a24SChandler Carruth   }
1376*ef860a24SChandler Carruth   return CurIdx == IdxList.size() ? Agg : 0;
1377*ef860a24SChandler Carruth }
1378*ef860a24SChandler Carruth 
1379*ef860a24SChandler Carruth Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList) {
1380*ef860a24SChandler Carruth   return getIndexedTypeInternal(Ptr, IdxList);
1381*ef860a24SChandler Carruth }
1382*ef860a24SChandler Carruth 
1383*ef860a24SChandler Carruth Type *GetElementPtrInst::getIndexedType(Type *Ptr,
1384*ef860a24SChandler Carruth                                         ArrayRef<Constant *> IdxList) {
1385*ef860a24SChandler Carruth   return getIndexedTypeInternal(Ptr, IdxList);
1386*ef860a24SChandler Carruth }
1387*ef860a24SChandler Carruth 
1388*ef860a24SChandler Carruth Type *GetElementPtrInst::getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList) {
1389*ef860a24SChandler Carruth   return getIndexedTypeInternal(Ptr, IdxList);
1390*ef860a24SChandler Carruth }
1391*ef860a24SChandler Carruth 
1392*ef860a24SChandler Carruth /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1393*ef860a24SChandler Carruth /// zeros.  If so, the result pointer and the first operand have the same
1394*ef860a24SChandler Carruth /// value, just potentially different types.
1395*ef860a24SChandler Carruth bool GetElementPtrInst::hasAllZeroIndices() const {
1396*ef860a24SChandler Carruth   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1397*ef860a24SChandler Carruth     if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1398*ef860a24SChandler Carruth       if (!CI->isZero()) return false;
1399*ef860a24SChandler Carruth     } else {
1400*ef860a24SChandler Carruth       return false;
1401*ef860a24SChandler Carruth     }
1402*ef860a24SChandler Carruth   }
1403*ef860a24SChandler Carruth   return true;
1404*ef860a24SChandler Carruth }
1405*ef860a24SChandler Carruth 
1406*ef860a24SChandler Carruth /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1407*ef860a24SChandler Carruth /// constant integers.  If so, the result pointer and the first operand have
1408*ef860a24SChandler Carruth /// a constant offset between them.
1409*ef860a24SChandler Carruth bool GetElementPtrInst::hasAllConstantIndices() const {
1410*ef860a24SChandler Carruth   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1411*ef860a24SChandler Carruth     if (!isa<ConstantInt>(getOperand(i)))
1412*ef860a24SChandler Carruth       return false;
1413*ef860a24SChandler Carruth   }
1414*ef860a24SChandler Carruth   return true;
1415*ef860a24SChandler Carruth }
1416*ef860a24SChandler Carruth 
1417*ef860a24SChandler Carruth void GetElementPtrInst::setIsInBounds(bool B) {
1418*ef860a24SChandler Carruth   cast<GEPOperator>(this)->setIsInBounds(B);
1419*ef860a24SChandler Carruth }
1420*ef860a24SChandler Carruth 
1421*ef860a24SChandler Carruth bool GetElementPtrInst::isInBounds() const {
1422*ef860a24SChandler Carruth   return cast<GEPOperator>(this)->isInBounds();
1423*ef860a24SChandler Carruth }
1424*ef860a24SChandler Carruth 
1425*ef860a24SChandler Carruth bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1426*ef860a24SChandler Carruth                                                  APInt &Offset) const {
1427*ef860a24SChandler Carruth   // Delegate to the generic GEPOperator implementation.
1428*ef860a24SChandler Carruth   return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
1429*ef860a24SChandler Carruth }
1430*ef860a24SChandler Carruth 
1431*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1432*ef860a24SChandler Carruth //                           ExtractElementInst Implementation
1433*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1434*ef860a24SChandler Carruth 
1435*ef860a24SChandler Carruth ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1436*ef860a24SChandler Carruth                                        const Twine &Name,
1437*ef860a24SChandler Carruth                                        Instruction *InsertBef)
1438*ef860a24SChandler Carruth   : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1439*ef860a24SChandler Carruth                 ExtractElement,
1440*ef860a24SChandler Carruth                 OperandTraits<ExtractElementInst>::op_begin(this),
1441*ef860a24SChandler Carruth                 2, InsertBef) {
1442*ef860a24SChandler Carruth   assert(isValidOperands(Val, Index) &&
1443*ef860a24SChandler Carruth          "Invalid extractelement instruction operands!");
1444*ef860a24SChandler Carruth   Op<0>() = Val;
1445*ef860a24SChandler Carruth   Op<1>() = Index;
1446*ef860a24SChandler Carruth   setName(Name);
1447*ef860a24SChandler Carruth }
1448*ef860a24SChandler Carruth 
1449*ef860a24SChandler Carruth ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
1450*ef860a24SChandler Carruth                                        const Twine &Name,
1451*ef860a24SChandler Carruth                                        BasicBlock *InsertAE)
1452*ef860a24SChandler Carruth   : Instruction(cast<VectorType>(Val->getType())->getElementType(),
1453*ef860a24SChandler Carruth                 ExtractElement,
1454*ef860a24SChandler Carruth                 OperandTraits<ExtractElementInst>::op_begin(this),
1455*ef860a24SChandler Carruth                 2, InsertAE) {
1456*ef860a24SChandler Carruth   assert(isValidOperands(Val, Index) &&
1457*ef860a24SChandler Carruth          "Invalid extractelement instruction operands!");
1458*ef860a24SChandler Carruth 
1459*ef860a24SChandler Carruth   Op<0>() = Val;
1460*ef860a24SChandler Carruth   Op<1>() = Index;
1461*ef860a24SChandler Carruth   setName(Name);
1462*ef860a24SChandler Carruth }
1463*ef860a24SChandler Carruth 
1464*ef860a24SChandler Carruth 
1465*ef860a24SChandler Carruth bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
1466*ef860a24SChandler Carruth   if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
1467*ef860a24SChandler Carruth     return false;
1468*ef860a24SChandler Carruth   return true;
1469*ef860a24SChandler Carruth }
1470*ef860a24SChandler Carruth 
1471*ef860a24SChandler Carruth 
1472*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1473*ef860a24SChandler Carruth //                           InsertElementInst Implementation
1474*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1475*ef860a24SChandler Carruth 
1476*ef860a24SChandler Carruth InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1477*ef860a24SChandler Carruth                                      const Twine &Name,
1478*ef860a24SChandler Carruth                                      Instruction *InsertBef)
1479*ef860a24SChandler Carruth   : Instruction(Vec->getType(), InsertElement,
1480*ef860a24SChandler Carruth                 OperandTraits<InsertElementInst>::op_begin(this),
1481*ef860a24SChandler Carruth                 3, InsertBef) {
1482*ef860a24SChandler Carruth   assert(isValidOperands(Vec, Elt, Index) &&
1483*ef860a24SChandler Carruth          "Invalid insertelement instruction operands!");
1484*ef860a24SChandler Carruth   Op<0>() = Vec;
1485*ef860a24SChandler Carruth   Op<1>() = Elt;
1486*ef860a24SChandler Carruth   Op<2>() = Index;
1487*ef860a24SChandler Carruth   setName(Name);
1488*ef860a24SChandler Carruth }
1489*ef860a24SChandler Carruth 
1490*ef860a24SChandler Carruth InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
1491*ef860a24SChandler Carruth                                      const Twine &Name,
1492*ef860a24SChandler Carruth                                      BasicBlock *InsertAE)
1493*ef860a24SChandler Carruth   : Instruction(Vec->getType(), InsertElement,
1494*ef860a24SChandler Carruth                 OperandTraits<InsertElementInst>::op_begin(this),
1495*ef860a24SChandler Carruth                 3, InsertAE) {
1496*ef860a24SChandler Carruth   assert(isValidOperands(Vec, Elt, Index) &&
1497*ef860a24SChandler Carruth          "Invalid insertelement instruction operands!");
1498*ef860a24SChandler Carruth 
1499*ef860a24SChandler Carruth   Op<0>() = Vec;
1500*ef860a24SChandler Carruth   Op<1>() = Elt;
1501*ef860a24SChandler Carruth   Op<2>() = Index;
1502*ef860a24SChandler Carruth   setName(Name);
1503*ef860a24SChandler Carruth }
1504*ef860a24SChandler Carruth 
1505*ef860a24SChandler Carruth bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1506*ef860a24SChandler Carruth                                         const Value *Index) {
1507*ef860a24SChandler Carruth   if (!Vec->getType()->isVectorTy())
1508*ef860a24SChandler Carruth     return false;   // First operand of insertelement must be vector type.
1509*ef860a24SChandler Carruth 
1510*ef860a24SChandler Carruth   if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
1511*ef860a24SChandler Carruth     return false;// Second operand of insertelement must be vector element type.
1512*ef860a24SChandler Carruth 
1513*ef860a24SChandler Carruth   if (!Index->getType()->isIntegerTy(32))
1514*ef860a24SChandler Carruth     return false;  // Third operand of insertelement must be i32.
1515*ef860a24SChandler Carruth   return true;
1516*ef860a24SChandler Carruth }
1517*ef860a24SChandler Carruth 
1518*ef860a24SChandler Carruth 
1519*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1520*ef860a24SChandler Carruth //                      ShuffleVectorInst Implementation
1521*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1522*ef860a24SChandler Carruth 
1523*ef860a24SChandler Carruth ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1524*ef860a24SChandler Carruth                                      const Twine &Name,
1525*ef860a24SChandler Carruth                                      Instruction *InsertBefore)
1526*ef860a24SChandler Carruth : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1527*ef860a24SChandler Carruth                 cast<VectorType>(Mask->getType())->getNumElements()),
1528*ef860a24SChandler Carruth               ShuffleVector,
1529*ef860a24SChandler Carruth               OperandTraits<ShuffleVectorInst>::op_begin(this),
1530*ef860a24SChandler Carruth               OperandTraits<ShuffleVectorInst>::operands(this),
1531*ef860a24SChandler Carruth               InsertBefore) {
1532*ef860a24SChandler Carruth   assert(isValidOperands(V1, V2, Mask) &&
1533*ef860a24SChandler Carruth          "Invalid shuffle vector instruction operands!");
1534*ef860a24SChandler Carruth   Op<0>() = V1;
1535*ef860a24SChandler Carruth   Op<1>() = V2;
1536*ef860a24SChandler Carruth   Op<2>() = Mask;
1537*ef860a24SChandler Carruth   setName(Name);
1538*ef860a24SChandler Carruth }
1539*ef860a24SChandler Carruth 
1540*ef860a24SChandler Carruth ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1541*ef860a24SChandler Carruth                                      const Twine &Name,
1542*ef860a24SChandler Carruth                                      BasicBlock *InsertAtEnd)
1543*ef860a24SChandler Carruth : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1544*ef860a24SChandler Carruth                 cast<VectorType>(Mask->getType())->getNumElements()),
1545*ef860a24SChandler Carruth               ShuffleVector,
1546*ef860a24SChandler Carruth               OperandTraits<ShuffleVectorInst>::op_begin(this),
1547*ef860a24SChandler Carruth               OperandTraits<ShuffleVectorInst>::operands(this),
1548*ef860a24SChandler Carruth               InsertAtEnd) {
1549*ef860a24SChandler Carruth   assert(isValidOperands(V1, V2, Mask) &&
1550*ef860a24SChandler Carruth          "Invalid shuffle vector instruction operands!");
1551*ef860a24SChandler Carruth 
1552*ef860a24SChandler Carruth   Op<0>() = V1;
1553*ef860a24SChandler Carruth   Op<1>() = V2;
1554*ef860a24SChandler Carruth   Op<2>() = Mask;
1555*ef860a24SChandler Carruth   setName(Name);
1556*ef860a24SChandler Carruth }
1557*ef860a24SChandler Carruth 
1558*ef860a24SChandler Carruth bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1559*ef860a24SChandler Carruth                                         const Value *Mask) {
1560*ef860a24SChandler Carruth   // V1 and V2 must be vectors of the same type.
1561*ef860a24SChandler Carruth   if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
1562*ef860a24SChandler Carruth     return false;
1563*ef860a24SChandler Carruth 
1564*ef860a24SChandler Carruth   // Mask must be vector of i32.
1565*ef860a24SChandler Carruth   VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1566*ef860a24SChandler Carruth   if (MaskTy == 0 || !MaskTy->getElementType()->isIntegerTy(32))
1567*ef860a24SChandler Carruth     return false;
1568*ef860a24SChandler Carruth 
1569*ef860a24SChandler Carruth   // Check to see if Mask is valid.
1570*ef860a24SChandler Carruth   if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1571*ef860a24SChandler Carruth     return true;
1572*ef860a24SChandler Carruth 
1573*ef860a24SChandler Carruth   if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
1574*ef860a24SChandler Carruth     unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1575*ef860a24SChandler Carruth     for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
1576*ef860a24SChandler Carruth       if (ConstantInt *CI = dyn_cast<ConstantInt>(MV->getOperand(i))) {
1577*ef860a24SChandler Carruth         if (CI->uge(V1Size*2))
1578*ef860a24SChandler Carruth           return false;
1579*ef860a24SChandler Carruth       } else if (!isa<UndefValue>(MV->getOperand(i))) {
1580*ef860a24SChandler Carruth         return false;
1581*ef860a24SChandler Carruth       }
1582*ef860a24SChandler Carruth     }
1583*ef860a24SChandler Carruth     return true;
1584*ef860a24SChandler Carruth   }
1585*ef860a24SChandler Carruth 
1586*ef860a24SChandler Carruth   if (const ConstantDataSequential *CDS =
1587*ef860a24SChandler Carruth         dyn_cast<ConstantDataSequential>(Mask)) {
1588*ef860a24SChandler Carruth     unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1589*ef860a24SChandler Carruth     for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1590*ef860a24SChandler Carruth       if (CDS->getElementAsInteger(i) >= V1Size*2)
1591*ef860a24SChandler Carruth         return false;
1592*ef860a24SChandler Carruth     return true;
1593*ef860a24SChandler Carruth   }
1594*ef860a24SChandler Carruth 
1595*ef860a24SChandler Carruth   // The bitcode reader can create a place holder for a forward reference
1596*ef860a24SChandler Carruth   // used as the shuffle mask. When this occurs, the shuffle mask will
1597*ef860a24SChandler Carruth   // fall into this case and fail. To avoid this error, do this bit of
1598*ef860a24SChandler Carruth   // ugliness to allow such a mask pass.
1599*ef860a24SChandler Carruth   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1600*ef860a24SChandler Carruth     if (CE->getOpcode() == Instruction::UserOp1)
1601*ef860a24SChandler Carruth       return true;
1602*ef860a24SChandler Carruth 
1603*ef860a24SChandler Carruth   return false;
1604*ef860a24SChandler Carruth }
1605*ef860a24SChandler Carruth 
1606*ef860a24SChandler Carruth /// getMaskValue - Return the index from the shuffle mask for the specified
1607*ef860a24SChandler Carruth /// output result.  This is either -1 if the element is undef or a number less
1608*ef860a24SChandler Carruth /// than 2*numelements.
1609*ef860a24SChandler Carruth int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1610*ef860a24SChandler Carruth   assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1611*ef860a24SChandler Carruth   if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
1612*ef860a24SChandler Carruth     return CDS->getElementAsInteger(i);
1613*ef860a24SChandler Carruth   Constant *C = Mask->getAggregateElement(i);
1614*ef860a24SChandler Carruth   if (isa<UndefValue>(C))
1615*ef860a24SChandler Carruth     return -1;
1616*ef860a24SChandler Carruth   return cast<ConstantInt>(C)->getZExtValue();
1617*ef860a24SChandler Carruth }
1618*ef860a24SChandler Carruth 
1619*ef860a24SChandler Carruth /// getShuffleMask - Return the full mask for this instruction, where each
1620*ef860a24SChandler Carruth /// element is the element number and undef's are returned as -1.
1621*ef860a24SChandler Carruth void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1622*ef860a24SChandler Carruth                                        SmallVectorImpl<int> &Result) {
1623*ef860a24SChandler Carruth   unsigned NumElts = Mask->getType()->getVectorNumElements();
1624*ef860a24SChandler Carruth 
1625*ef860a24SChandler Carruth   if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
1626*ef860a24SChandler Carruth     for (unsigned i = 0; i != NumElts; ++i)
1627*ef860a24SChandler Carruth       Result.push_back(CDS->getElementAsInteger(i));
1628*ef860a24SChandler Carruth     return;
1629*ef860a24SChandler Carruth   }
1630*ef860a24SChandler Carruth   for (unsigned i = 0; i != NumElts; ++i) {
1631*ef860a24SChandler Carruth     Constant *C = Mask->getAggregateElement(i);
1632*ef860a24SChandler Carruth     Result.push_back(isa<UndefValue>(C) ? -1 :
1633*ef860a24SChandler Carruth                      cast<ConstantInt>(C)->getZExtValue());
1634*ef860a24SChandler Carruth   }
1635*ef860a24SChandler Carruth }
1636*ef860a24SChandler Carruth 
1637*ef860a24SChandler Carruth 
1638*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1639*ef860a24SChandler Carruth //                             InsertValueInst Class
1640*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1641*ef860a24SChandler Carruth 
1642*ef860a24SChandler Carruth void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1643*ef860a24SChandler Carruth                            const Twine &Name) {
1644*ef860a24SChandler Carruth   assert(NumOperands == 2 && "NumOperands not initialized?");
1645*ef860a24SChandler Carruth 
1646*ef860a24SChandler Carruth   // There's no fundamental reason why we require at least one index
1647*ef860a24SChandler Carruth   // (other than weirdness with &*IdxBegin being invalid; see
1648*ef860a24SChandler Carruth   // getelementptr's init routine for example). But there's no
1649*ef860a24SChandler Carruth   // present need to support it.
1650*ef860a24SChandler Carruth   assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1651*ef860a24SChandler Carruth 
1652*ef860a24SChandler Carruth   assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
1653*ef860a24SChandler Carruth          Val->getType() && "Inserted value must match indexed type!");
1654*ef860a24SChandler Carruth   Op<0>() = Agg;
1655*ef860a24SChandler Carruth   Op<1>() = Val;
1656*ef860a24SChandler Carruth 
1657*ef860a24SChandler Carruth   Indices.append(Idxs.begin(), Idxs.end());
1658*ef860a24SChandler Carruth   setName(Name);
1659*ef860a24SChandler Carruth }
1660*ef860a24SChandler Carruth 
1661*ef860a24SChandler Carruth InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
1662*ef860a24SChandler Carruth   : Instruction(IVI.getType(), InsertValue,
1663*ef860a24SChandler Carruth                 OperandTraits<InsertValueInst>::op_begin(this), 2),
1664*ef860a24SChandler Carruth     Indices(IVI.Indices) {
1665*ef860a24SChandler Carruth   Op<0>() = IVI.getOperand(0);
1666*ef860a24SChandler Carruth   Op<1>() = IVI.getOperand(1);
1667*ef860a24SChandler Carruth   SubclassOptionalData = IVI.SubclassOptionalData;
1668*ef860a24SChandler Carruth }
1669*ef860a24SChandler Carruth 
1670*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1671*ef860a24SChandler Carruth //                             ExtractValueInst Class
1672*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1673*ef860a24SChandler Carruth 
1674*ef860a24SChandler Carruth void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
1675*ef860a24SChandler Carruth   assert(NumOperands == 1 && "NumOperands not initialized?");
1676*ef860a24SChandler Carruth 
1677*ef860a24SChandler Carruth   // There's no fundamental reason why we require at least one index.
1678*ef860a24SChandler Carruth   // But there's no present need to support it.
1679*ef860a24SChandler Carruth   assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
1680*ef860a24SChandler Carruth 
1681*ef860a24SChandler Carruth   Indices.append(Idxs.begin(), Idxs.end());
1682*ef860a24SChandler Carruth   setName(Name);
1683*ef860a24SChandler Carruth }
1684*ef860a24SChandler Carruth 
1685*ef860a24SChandler Carruth ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
1686*ef860a24SChandler Carruth   : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
1687*ef860a24SChandler Carruth     Indices(EVI.Indices) {
1688*ef860a24SChandler Carruth   SubclassOptionalData = EVI.SubclassOptionalData;
1689*ef860a24SChandler Carruth }
1690*ef860a24SChandler Carruth 
1691*ef860a24SChandler Carruth // getIndexedType - Returns the type of the element that would be extracted
1692*ef860a24SChandler Carruth // with an extractvalue instruction with the specified parameters.
1693*ef860a24SChandler Carruth //
1694*ef860a24SChandler Carruth // A null type is returned if the indices are invalid for the specified
1695*ef860a24SChandler Carruth // pointer type.
1696*ef860a24SChandler Carruth //
1697*ef860a24SChandler Carruth Type *ExtractValueInst::getIndexedType(Type *Agg,
1698*ef860a24SChandler Carruth                                        ArrayRef<unsigned> Idxs) {
1699*ef860a24SChandler Carruth   for (unsigned CurIdx = 0; CurIdx != Idxs.size(); ++CurIdx) {
1700*ef860a24SChandler Carruth     unsigned Index = Idxs[CurIdx];
1701*ef860a24SChandler Carruth     // We can't use CompositeType::indexValid(Index) here.
1702*ef860a24SChandler Carruth     // indexValid() always returns true for arrays because getelementptr allows
1703*ef860a24SChandler Carruth     // out-of-bounds indices. Since we don't allow those for extractvalue and
1704*ef860a24SChandler Carruth     // insertvalue we need to check array indexing manually.
1705*ef860a24SChandler Carruth     // Since the only other types we can index into are struct types it's just
1706*ef860a24SChandler Carruth     // as easy to check those manually as well.
1707*ef860a24SChandler Carruth     if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
1708*ef860a24SChandler Carruth       if (Index >= AT->getNumElements())
1709*ef860a24SChandler Carruth         return 0;
1710*ef860a24SChandler Carruth     } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
1711*ef860a24SChandler Carruth       if (Index >= ST->getNumElements())
1712*ef860a24SChandler Carruth         return 0;
1713*ef860a24SChandler Carruth     } else {
1714*ef860a24SChandler Carruth       // Not a valid type to index into.
1715*ef860a24SChandler Carruth       return 0;
1716*ef860a24SChandler Carruth     }
1717*ef860a24SChandler Carruth 
1718*ef860a24SChandler Carruth     Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
1719*ef860a24SChandler Carruth   }
1720*ef860a24SChandler Carruth   return const_cast<Type*>(Agg);
1721*ef860a24SChandler Carruth }
1722*ef860a24SChandler Carruth 
1723*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1724*ef860a24SChandler Carruth //                             BinaryOperator Class
1725*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
1726*ef860a24SChandler Carruth 
1727*ef860a24SChandler Carruth BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1728*ef860a24SChandler Carruth                                Type *Ty, const Twine &Name,
1729*ef860a24SChandler Carruth                                Instruction *InsertBefore)
1730*ef860a24SChandler Carruth   : Instruction(Ty, iType,
1731*ef860a24SChandler Carruth                 OperandTraits<BinaryOperator>::op_begin(this),
1732*ef860a24SChandler Carruth                 OperandTraits<BinaryOperator>::operands(this),
1733*ef860a24SChandler Carruth                 InsertBefore) {
1734*ef860a24SChandler Carruth   Op<0>() = S1;
1735*ef860a24SChandler Carruth   Op<1>() = S2;
1736*ef860a24SChandler Carruth   init(iType);
1737*ef860a24SChandler Carruth   setName(Name);
1738*ef860a24SChandler Carruth }
1739*ef860a24SChandler Carruth 
1740*ef860a24SChandler Carruth BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1741*ef860a24SChandler Carruth                                Type *Ty, const Twine &Name,
1742*ef860a24SChandler Carruth                                BasicBlock *InsertAtEnd)
1743*ef860a24SChandler Carruth   : Instruction(Ty, iType,
1744*ef860a24SChandler Carruth                 OperandTraits<BinaryOperator>::op_begin(this),
1745*ef860a24SChandler Carruth                 OperandTraits<BinaryOperator>::operands(this),
1746*ef860a24SChandler Carruth                 InsertAtEnd) {
1747*ef860a24SChandler Carruth   Op<0>() = S1;
1748*ef860a24SChandler Carruth   Op<1>() = S2;
1749*ef860a24SChandler Carruth   init(iType);
1750*ef860a24SChandler Carruth   setName(Name);
1751*ef860a24SChandler Carruth }
1752*ef860a24SChandler Carruth 
1753*ef860a24SChandler Carruth 
1754*ef860a24SChandler Carruth void BinaryOperator::init(BinaryOps iType) {
1755*ef860a24SChandler Carruth   Value *LHS = getOperand(0), *RHS = getOperand(1);
1756*ef860a24SChandler Carruth   (void)LHS; (void)RHS; // Silence warnings.
1757*ef860a24SChandler Carruth   assert(LHS->getType() == RHS->getType() &&
1758*ef860a24SChandler Carruth          "Binary operator operand types must match!");
1759*ef860a24SChandler Carruth #ifndef NDEBUG
1760*ef860a24SChandler Carruth   switch (iType) {
1761*ef860a24SChandler Carruth   case Add: case Sub:
1762*ef860a24SChandler Carruth   case Mul:
1763*ef860a24SChandler Carruth     assert(getType() == LHS->getType() &&
1764*ef860a24SChandler Carruth            "Arithmetic operation should return same type as operands!");
1765*ef860a24SChandler Carruth     assert(getType()->isIntOrIntVectorTy() &&
1766*ef860a24SChandler Carruth            "Tried to create an integer operation on a non-integer type!");
1767*ef860a24SChandler Carruth     break;
1768*ef860a24SChandler Carruth   case FAdd: case FSub:
1769*ef860a24SChandler Carruth   case FMul:
1770*ef860a24SChandler Carruth     assert(getType() == LHS->getType() &&
1771*ef860a24SChandler Carruth            "Arithmetic operation should return same type as operands!");
1772*ef860a24SChandler Carruth     assert(getType()->isFPOrFPVectorTy() &&
1773*ef860a24SChandler Carruth            "Tried to create a floating-point operation on a "
1774*ef860a24SChandler Carruth            "non-floating-point type!");
1775*ef860a24SChandler Carruth     break;
1776*ef860a24SChandler Carruth   case UDiv:
1777*ef860a24SChandler Carruth   case SDiv:
1778*ef860a24SChandler Carruth     assert(getType() == LHS->getType() &&
1779*ef860a24SChandler Carruth            "Arithmetic operation should return same type as operands!");
1780*ef860a24SChandler Carruth     assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1781*ef860a24SChandler Carruth             cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1782*ef860a24SChandler Carruth            "Incorrect operand type (not integer) for S/UDIV");
1783*ef860a24SChandler Carruth     break;
1784*ef860a24SChandler Carruth   case FDiv:
1785*ef860a24SChandler Carruth     assert(getType() == LHS->getType() &&
1786*ef860a24SChandler Carruth            "Arithmetic operation should return same type as operands!");
1787*ef860a24SChandler Carruth     assert(getType()->isFPOrFPVectorTy() &&
1788*ef860a24SChandler Carruth            "Incorrect operand type (not floating point) for FDIV");
1789*ef860a24SChandler Carruth     break;
1790*ef860a24SChandler Carruth   case URem:
1791*ef860a24SChandler Carruth   case SRem:
1792*ef860a24SChandler Carruth     assert(getType() == LHS->getType() &&
1793*ef860a24SChandler Carruth            "Arithmetic operation should return same type as operands!");
1794*ef860a24SChandler Carruth     assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
1795*ef860a24SChandler Carruth             cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1796*ef860a24SChandler Carruth            "Incorrect operand type (not integer) for S/UREM");
1797*ef860a24SChandler Carruth     break;
1798*ef860a24SChandler Carruth   case FRem:
1799*ef860a24SChandler Carruth     assert(getType() == LHS->getType() &&
1800*ef860a24SChandler Carruth            "Arithmetic operation should return same type as operands!");
1801*ef860a24SChandler Carruth     assert(getType()->isFPOrFPVectorTy() &&
1802*ef860a24SChandler Carruth            "Incorrect operand type (not floating point) for FREM");
1803*ef860a24SChandler Carruth     break;
1804*ef860a24SChandler Carruth   case Shl:
1805*ef860a24SChandler Carruth   case LShr:
1806*ef860a24SChandler Carruth   case AShr:
1807*ef860a24SChandler Carruth     assert(getType() == LHS->getType() &&
1808*ef860a24SChandler Carruth            "Shift operation should return same type as operands!");
1809*ef860a24SChandler Carruth     assert((getType()->isIntegerTy() ||
1810*ef860a24SChandler Carruth             (getType()->isVectorTy() &&
1811*ef860a24SChandler Carruth              cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1812*ef860a24SChandler Carruth            "Tried to create a shift operation on a non-integral type!");
1813*ef860a24SChandler Carruth     break;
1814*ef860a24SChandler Carruth   case And: case Or:
1815*ef860a24SChandler Carruth   case Xor:
1816*ef860a24SChandler Carruth     assert(getType() == LHS->getType() &&
1817*ef860a24SChandler Carruth            "Logical operation should return same type as operands!");
1818*ef860a24SChandler Carruth     assert((getType()->isIntegerTy() ||
1819*ef860a24SChandler Carruth             (getType()->isVectorTy() &&
1820*ef860a24SChandler Carruth              cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
1821*ef860a24SChandler Carruth            "Tried to create a logical operation on a non-integral type!");
1822*ef860a24SChandler Carruth     break;
1823*ef860a24SChandler Carruth   default:
1824*ef860a24SChandler Carruth     break;
1825*ef860a24SChandler Carruth   }
1826*ef860a24SChandler Carruth #endif
1827*ef860a24SChandler Carruth }
1828*ef860a24SChandler Carruth 
1829*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1830*ef860a24SChandler Carruth                                        const Twine &Name,
1831*ef860a24SChandler Carruth                                        Instruction *InsertBefore) {
1832*ef860a24SChandler Carruth   assert(S1->getType() == S2->getType() &&
1833*ef860a24SChandler Carruth          "Cannot create binary operator with two operands of differing type!");
1834*ef860a24SChandler Carruth   return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
1835*ef860a24SChandler Carruth }
1836*ef860a24SChandler Carruth 
1837*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
1838*ef860a24SChandler Carruth                                        const Twine &Name,
1839*ef860a24SChandler Carruth                                        BasicBlock *InsertAtEnd) {
1840*ef860a24SChandler Carruth   BinaryOperator *Res = Create(Op, S1, S2, Name);
1841*ef860a24SChandler Carruth   InsertAtEnd->getInstList().push_back(Res);
1842*ef860a24SChandler Carruth   return Res;
1843*ef860a24SChandler Carruth }
1844*ef860a24SChandler Carruth 
1845*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1846*ef860a24SChandler Carruth                                           Instruction *InsertBefore) {
1847*ef860a24SChandler Carruth   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1848*ef860a24SChandler Carruth   return new BinaryOperator(Instruction::Sub,
1849*ef860a24SChandler Carruth                             zero, Op,
1850*ef860a24SChandler Carruth                             Op->getType(), Name, InsertBefore);
1851*ef860a24SChandler Carruth }
1852*ef860a24SChandler Carruth 
1853*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
1854*ef860a24SChandler Carruth                                           BasicBlock *InsertAtEnd) {
1855*ef860a24SChandler Carruth   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1856*ef860a24SChandler Carruth   return new BinaryOperator(Instruction::Sub,
1857*ef860a24SChandler Carruth                             zero, Op,
1858*ef860a24SChandler Carruth                             Op->getType(), Name, InsertAtEnd);
1859*ef860a24SChandler Carruth }
1860*ef860a24SChandler Carruth 
1861*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1862*ef860a24SChandler Carruth                                              Instruction *InsertBefore) {
1863*ef860a24SChandler Carruth   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1864*ef860a24SChandler Carruth   return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1865*ef860a24SChandler Carruth }
1866*ef860a24SChandler Carruth 
1867*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1868*ef860a24SChandler Carruth                                              BasicBlock *InsertAtEnd) {
1869*ef860a24SChandler Carruth   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1870*ef860a24SChandler Carruth   return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1871*ef860a24SChandler Carruth }
1872*ef860a24SChandler Carruth 
1873*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1874*ef860a24SChandler Carruth                                              Instruction *InsertBefore) {
1875*ef860a24SChandler Carruth   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1876*ef860a24SChandler Carruth   return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1877*ef860a24SChandler Carruth }
1878*ef860a24SChandler Carruth 
1879*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1880*ef860a24SChandler Carruth                                              BasicBlock *InsertAtEnd) {
1881*ef860a24SChandler Carruth   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1882*ef860a24SChandler Carruth   return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1883*ef860a24SChandler Carruth }
1884*ef860a24SChandler Carruth 
1885*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1886*ef860a24SChandler Carruth                                            Instruction *InsertBefore) {
1887*ef860a24SChandler Carruth   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1888*ef860a24SChandler Carruth   return new BinaryOperator(Instruction::FSub, zero, Op,
1889*ef860a24SChandler Carruth                             Op->getType(), Name, InsertBefore);
1890*ef860a24SChandler Carruth }
1891*ef860a24SChandler Carruth 
1892*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
1893*ef860a24SChandler Carruth                                            BasicBlock *InsertAtEnd) {
1894*ef860a24SChandler Carruth   Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1895*ef860a24SChandler Carruth   return new BinaryOperator(Instruction::FSub, zero, Op,
1896*ef860a24SChandler Carruth                             Op->getType(), Name, InsertAtEnd);
1897*ef860a24SChandler Carruth }
1898*ef860a24SChandler Carruth 
1899*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1900*ef860a24SChandler Carruth                                           Instruction *InsertBefore) {
1901*ef860a24SChandler Carruth   Constant *C = Constant::getAllOnesValue(Op->getType());
1902*ef860a24SChandler Carruth   return new BinaryOperator(Instruction::Xor, Op, C,
1903*ef860a24SChandler Carruth                             Op->getType(), Name, InsertBefore);
1904*ef860a24SChandler Carruth }
1905*ef860a24SChandler Carruth 
1906*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
1907*ef860a24SChandler Carruth                                           BasicBlock *InsertAtEnd) {
1908*ef860a24SChandler Carruth   Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
1909*ef860a24SChandler Carruth   return new BinaryOperator(Instruction::Xor, Op, AllOnes,
1910*ef860a24SChandler Carruth                             Op->getType(), Name, InsertAtEnd);
1911*ef860a24SChandler Carruth }
1912*ef860a24SChandler Carruth 
1913*ef860a24SChandler Carruth 
1914*ef860a24SChandler Carruth // isConstantAllOnes - Helper function for several functions below
1915*ef860a24SChandler Carruth static inline bool isConstantAllOnes(const Value *V) {
1916*ef860a24SChandler Carruth   if (const Constant *C = dyn_cast<Constant>(V))
1917*ef860a24SChandler Carruth     return C->isAllOnesValue();
1918*ef860a24SChandler Carruth   return false;
1919*ef860a24SChandler Carruth }
1920*ef860a24SChandler Carruth 
1921*ef860a24SChandler Carruth bool BinaryOperator::isNeg(const Value *V) {
1922*ef860a24SChandler Carruth   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1923*ef860a24SChandler Carruth     if (Bop->getOpcode() == Instruction::Sub)
1924*ef860a24SChandler Carruth       if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1925*ef860a24SChandler Carruth         return C->isNegativeZeroValue();
1926*ef860a24SChandler Carruth   return false;
1927*ef860a24SChandler Carruth }
1928*ef860a24SChandler Carruth 
1929*ef860a24SChandler Carruth bool BinaryOperator::isFNeg(const Value *V) {
1930*ef860a24SChandler Carruth   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1931*ef860a24SChandler Carruth     if (Bop->getOpcode() == Instruction::FSub)
1932*ef860a24SChandler Carruth       if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1933*ef860a24SChandler Carruth         return C->isNegativeZeroValue();
1934*ef860a24SChandler Carruth   return false;
1935*ef860a24SChandler Carruth }
1936*ef860a24SChandler Carruth 
1937*ef860a24SChandler Carruth bool BinaryOperator::isNot(const Value *V) {
1938*ef860a24SChandler Carruth   if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1939*ef860a24SChandler Carruth     return (Bop->getOpcode() == Instruction::Xor &&
1940*ef860a24SChandler Carruth             (isConstantAllOnes(Bop->getOperand(1)) ||
1941*ef860a24SChandler Carruth              isConstantAllOnes(Bop->getOperand(0))));
1942*ef860a24SChandler Carruth   return false;
1943*ef860a24SChandler Carruth }
1944*ef860a24SChandler Carruth 
1945*ef860a24SChandler Carruth Value *BinaryOperator::getNegArgument(Value *BinOp) {
1946*ef860a24SChandler Carruth   return cast<BinaryOperator>(BinOp)->getOperand(1);
1947*ef860a24SChandler Carruth }
1948*ef860a24SChandler Carruth 
1949*ef860a24SChandler Carruth const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1950*ef860a24SChandler Carruth   return getNegArgument(const_cast<Value*>(BinOp));
1951*ef860a24SChandler Carruth }
1952*ef860a24SChandler Carruth 
1953*ef860a24SChandler Carruth Value *BinaryOperator::getFNegArgument(Value *BinOp) {
1954*ef860a24SChandler Carruth   return cast<BinaryOperator>(BinOp)->getOperand(1);
1955*ef860a24SChandler Carruth }
1956*ef860a24SChandler Carruth 
1957*ef860a24SChandler Carruth const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1958*ef860a24SChandler Carruth   return getFNegArgument(const_cast<Value*>(BinOp));
1959*ef860a24SChandler Carruth }
1960*ef860a24SChandler Carruth 
1961*ef860a24SChandler Carruth Value *BinaryOperator::getNotArgument(Value *BinOp) {
1962*ef860a24SChandler Carruth   assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1963*ef860a24SChandler Carruth   BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1964*ef860a24SChandler Carruth   Value *Op0 = BO->getOperand(0);
1965*ef860a24SChandler Carruth   Value *Op1 = BO->getOperand(1);
1966*ef860a24SChandler Carruth   if (isConstantAllOnes(Op0)) return Op1;
1967*ef860a24SChandler Carruth 
1968*ef860a24SChandler Carruth   assert(isConstantAllOnes(Op1));
1969*ef860a24SChandler Carruth   return Op0;
1970*ef860a24SChandler Carruth }
1971*ef860a24SChandler Carruth 
1972*ef860a24SChandler Carruth const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1973*ef860a24SChandler Carruth   return getNotArgument(const_cast<Value*>(BinOp));
1974*ef860a24SChandler Carruth }
1975*ef860a24SChandler Carruth 
1976*ef860a24SChandler Carruth 
1977*ef860a24SChandler Carruth // swapOperands - Exchange the two operands to this instruction.  This
1978*ef860a24SChandler Carruth // instruction is safe to use on any binary instruction and does not
1979*ef860a24SChandler Carruth // modify the semantics of the instruction.  If the instruction is
1980*ef860a24SChandler Carruth // order dependent (SetLT f.e.) the opcode is changed.
1981*ef860a24SChandler Carruth //
1982*ef860a24SChandler Carruth bool BinaryOperator::swapOperands() {
1983*ef860a24SChandler Carruth   if (!isCommutative())
1984*ef860a24SChandler Carruth     return true; // Can't commute operands
1985*ef860a24SChandler Carruth   Op<0>().swap(Op<1>());
1986*ef860a24SChandler Carruth   return false;
1987*ef860a24SChandler Carruth }
1988*ef860a24SChandler Carruth 
1989*ef860a24SChandler Carruth void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1990*ef860a24SChandler Carruth   cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1991*ef860a24SChandler Carruth }
1992*ef860a24SChandler Carruth 
1993*ef860a24SChandler Carruth void BinaryOperator::setHasNoSignedWrap(bool b) {
1994*ef860a24SChandler Carruth   cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1995*ef860a24SChandler Carruth }
1996*ef860a24SChandler Carruth 
1997*ef860a24SChandler Carruth void BinaryOperator::setIsExact(bool b) {
1998*ef860a24SChandler Carruth   cast<PossiblyExactOperator>(this)->setIsExact(b);
1999*ef860a24SChandler Carruth }
2000*ef860a24SChandler Carruth 
2001*ef860a24SChandler Carruth bool BinaryOperator::hasNoUnsignedWrap() const {
2002*ef860a24SChandler Carruth   return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2003*ef860a24SChandler Carruth }
2004*ef860a24SChandler Carruth 
2005*ef860a24SChandler Carruth bool BinaryOperator::hasNoSignedWrap() const {
2006*ef860a24SChandler Carruth   return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2007*ef860a24SChandler Carruth }
2008*ef860a24SChandler Carruth 
2009*ef860a24SChandler Carruth bool BinaryOperator::isExact() const {
2010*ef860a24SChandler Carruth   return cast<PossiblyExactOperator>(this)->isExact();
2011*ef860a24SChandler Carruth }
2012*ef860a24SChandler Carruth 
2013*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
2014*ef860a24SChandler Carruth //                             FPMathOperator Class
2015*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
2016*ef860a24SChandler Carruth 
2017*ef860a24SChandler Carruth /// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2018*ef860a24SChandler Carruth /// An accuracy of 0.0 means that the operation should be performed with the
2019*ef860a24SChandler Carruth /// default precision.
2020*ef860a24SChandler Carruth float FPMathOperator::getFPAccuracy() const {
2021*ef860a24SChandler Carruth   const MDNode *MD =
2022*ef860a24SChandler Carruth     cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
2023*ef860a24SChandler Carruth   if (!MD)
2024*ef860a24SChandler Carruth     return 0.0;
2025*ef860a24SChandler Carruth   ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
2026*ef860a24SChandler Carruth   return Accuracy->getValueAPF().convertToFloat();
2027*ef860a24SChandler Carruth }
2028*ef860a24SChandler Carruth 
2029*ef860a24SChandler Carruth 
2030*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
2031*ef860a24SChandler Carruth //                                CastInst Class
2032*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
2033*ef860a24SChandler Carruth 
2034*ef860a24SChandler Carruth void CastInst::anchor() {}
2035*ef860a24SChandler Carruth 
2036*ef860a24SChandler Carruth // Just determine if this cast only deals with integral->integral conversion.
2037*ef860a24SChandler Carruth bool CastInst::isIntegerCast() const {
2038*ef860a24SChandler Carruth   switch (getOpcode()) {
2039*ef860a24SChandler Carruth     default: return false;
2040*ef860a24SChandler Carruth     case Instruction::ZExt:
2041*ef860a24SChandler Carruth     case Instruction::SExt:
2042*ef860a24SChandler Carruth     case Instruction::Trunc:
2043*ef860a24SChandler Carruth       return true;
2044*ef860a24SChandler Carruth     case Instruction::BitCast:
2045*ef860a24SChandler Carruth       return getOperand(0)->getType()->isIntegerTy() &&
2046*ef860a24SChandler Carruth         getType()->isIntegerTy();
2047*ef860a24SChandler Carruth   }
2048*ef860a24SChandler Carruth }
2049*ef860a24SChandler Carruth 
2050*ef860a24SChandler Carruth bool CastInst::isLosslessCast() const {
2051*ef860a24SChandler Carruth   // Only BitCast can be lossless, exit fast if we're not BitCast
2052*ef860a24SChandler Carruth   if (getOpcode() != Instruction::BitCast)
2053*ef860a24SChandler Carruth     return false;
2054*ef860a24SChandler Carruth 
2055*ef860a24SChandler Carruth   // Identity cast is always lossless
2056*ef860a24SChandler Carruth   Type* SrcTy = getOperand(0)->getType();
2057*ef860a24SChandler Carruth   Type* DstTy = getType();
2058*ef860a24SChandler Carruth   if (SrcTy == DstTy)
2059*ef860a24SChandler Carruth     return true;
2060*ef860a24SChandler Carruth 
2061*ef860a24SChandler Carruth   // Pointer to pointer is always lossless.
2062*ef860a24SChandler Carruth   if (SrcTy->isPointerTy())
2063*ef860a24SChandler Carruth     return DstTy->isPointerTy();
2064*ef860a24SChandler Carruth   return false;  // Other types have no identity values
2065*ef860a24SChandler Carruth }
2066*ef860a24SChandler Carruth 
2067*ef860a24SChandler Carruth /// This function determines if the CastInst does not require any bits to be
2068*ef860a24SChandler Carruth /// changed in order to effect the cast. Essentially, it identifies cases where
2069*ef860a24SChandler Carruth /// no code gen is necessary for the cast, hence the name no-op cast.  For
2070*ef860a24SChandler Carruth /// example, the following are all no-op casts:
2071*ef860a24SChandler Carruth /// # bitcast i32* %x to i8*
2072*ef860a24SChandler Carruth /// # bitcast <2 x i32> %x to <4 x i16>
2073*ef860a24SChandler Carruth /// # ptrtoint i32* %x to i32     ; on 32-bit plaforms only
2074*ef860a24SChandler Carruth /// @brief Determine if the described cast is a no-op.
2075*ef860a24SChandler Carruth bool CastInst::isNoopCast(Instruction::CastOps Opcode,
2076*ef860a24SChandler Carruth                           Type *SrcTy,
2077*ef860a24SChandler Carruth                           Type *DestTy,
2078*ef860a24SChandler Carruth                           Type *IntPtrTy) {
2079*ef860a24SChandler Carruth   switch (Opcode) {
2080*ef860a24SChandler Carruth     default: llvm_unreachable("Invalid CastOp");
2081*ef860a24SChandler Carruth     case Instruction::Trunc:
2082*ef860a24SChandler Carruth     case Instruction::ZExt:
2083*ef860a24SChandler Carruth     case Instruction::SExt:
2084*ef860a24SChandler Carruth     case Instruction::FPTrunc:
2085*ef860a24SChandler Carruth     case Instruction::FPExt:
2086*ef860a24SChandler Carruth     case Instruction::UIToFP:
2087*ef860a24SChandler Carruth     case Instruction::SIToFP:
2088*ef860a24SChandler Carruth     case Instruction::FPToUI:
2089*ef860a24SChandler Carruth     case Instruction::FPToSI:
2090*ef860a24SChandler Carruth       return false; // These always modify bits
2091*ef860a24SChandler Carruth     case Instruction::BitCast:
2092*ef860a24SChandler Carruth       return true;  // BitCast never modifies bits.
2093*ef860a24SChandler Carruth     case Instruction::PtrToInt:
2094*ef860a24SChandler Carruth       return IntPtrTy->getScalarSizeInBits() ==
2095*ef860a24SChandler Carruth              DestTy->getScalarSizeInBits();
2096*ef860a24SChandler Carruth     case Instruction::IntToPtr:
2097*ef860a24SChandler Carruth       return IntPtrTy->getScalarSizeInBits() ==
2098*ef860a24SChandler Carruth              SrcTy->getScalarSizeInBits();
2099*ef860a24SChandler Carruth   }
2100*ef860a24SChandler Carruth }
2101*ef860a24SChandler Carruth 
2102*ef860a24SChandler Carruth /// @brief Determine if a cast is a no-op.
2103*ef860a24SChandler Carruth bool CastInst::isNoopCast(Type *IntPtrTy) const {
2104*ef860a24SChandler Carruth   return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2105*ef860a24SChandler Carruth }
2106*ef860a24SChandler Carruth 
2107*ef860a24SChandler Carruth /// This function determines if a pair of casts can be eliminated and what
2108*ef860a24SChandler Carruth /// opcode should be used in the elimination. This assumes that there are two
2109*ef860a24SChandler Carruth /// instructions like this:
2110*ef860a24SChandler Carruth /// *  %F = firstOpcode SrcTy %x to MidTy
2111*ef860a24SChandler Carruth /// *  %S = secondOpcode MidTy %F to DstTy
2112*ef860a24SChandler Carruth /// The function returns a resultOpcode so these two casts can be replaced with:
2113*ef860a24SChandler Carruth /// *  %Replacement = resultOpcode %SrcTy %x to DstTy
2114*ef860a24SChandler Carruth /// If no such cast is permited, the function returns 0.
2115*ef860a24SChandler Carruth unsigned CastInst::isEliminableCastPair(
2116*ef860a24SChandler Carruth   Instruction::CastOps firstOp, Instruction::CastOps secondOp,
2117*ef860a24SChandler Carruth   Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2118*ef860a24SChandler Carruth   Type *DstIntPtrTy) {
2119*ef860a24SChandler Carruth   // Define the 144 possibilities for these two cast instructions. The values
2120*ef860a24SChandler Carruth   // in this matrix determine what to do in a given situation and select the
2121*ef860a24SChandler Carruth   // case in the switch below.  The rows correspond to firstOp, the columns
2122*ef860a24SChandler Carruth   // correspond to secondOp.  In looking at the table below, keep in  mind
2123*ef860a24SChandler Carruth   // the following cast properties:
2124*ef860a24SChandler Carruth   //
2125*ef860a24SChandler Carruth   //          Size Compare       Source               Destination
2126*ef860a24SChandler Carruth   // Operator  Src ? Size   Type       Sign         Type       Sign
2127*ef860a24SChandler Carruth   // -------- ------------ -------------------   ---------------------
2128*ef860a24SChandler Carruth   // TRUNC         >       Integer      Any        Integral     Any
2129*ef860a24SChandler Carruth   // ZEXT          <       Integral   Unsigned     Integer      Any
2130*ef860a24SChandler Carruth   // SEXT          <       Integral    Signed      Integer      Any
2131*ef860a24SChandler Carruth   // FPTOUI       n/a      FloatPt      n/a        Integral   Unsigned
2132*ef860a24SChandler Carruth   // FPTOSI       n/a      FloatPt      n/a        Integral    Signed
2133*ef860a24SChandler Carruth   // UITOFP       n/a      Integral   Unsigned     FloatPt      n/a
2134*ef860a24SChandler Carruth   // SITOFP       n/a      Integral    Signed      FloatPt      n/a
2135*ef860a24SChandler Carruth   // FPTRUNC       >       FloatPt      n/a        FloatPt      n/a
2136*ef860a24SChandler Carruth   // FPEXT         <       FloatPt      n/a        FloatPt      n/a
2137*ef860a24SChandler Carruth   // PTRTOINT     n/a      Pointer      n/a        Integral   Unsigned
2138*ef860a24SChandler Carruth   // INTTOPTR     n/a      Integral   Unsigned     Pointer      n/a
2139*ef860a24SChandler Carruth   // BITCAST       =       FirstClass   n/a       FirstClass    n/a
2140*ef860a24SChandler Carruth   //
2141*ef860a24SChandler Carruth   // NOTE: some transforms are safe, but we consider them to be non-profitable.
2142*ef860a24SChandler Carruth   // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2143*ef860a24SChandler Carruth   // into "fptoui double to i64", but this loses information about the range
2144*ef860a24SChandler Carruth   // of the produced value (we no longer know the top-part is all zeros).
2145*ef860a24SChandler Carruth   // Further this conversion is often much more expensive for typical hardware,
2146*ef860a24SChandler Carruth   // and causes issues when building libgcc.  We disallow fptosi+sext for the
2147*ef860a24SChandler Carruth   // same reason.
2148*ef860a24SChandler Carruth   const unsigned numCastOps =
2149*ef860a24SChandler Carruth     Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2150*ef860a24SChandler Carruth   static const uint8_t CastResults[numCastOps][numCastOps] = {
2151*ef860a24SChandler Carruth     // T        F  F  U  S  F  F  P  I  B   -+
2152*ef860a24SChandler Carruth     // R  Z  S  P  P  I  I  T  P  2  N  T    |
2153*ef860a24SChandler Carruth     // U  E  E  2  2  2  2  R  E  I  T  C    +- secondOp
2154*ef860a24SChandler Carruth     // N  X  X  U  S  F  F  N  X  N  2  V    |
2155*ef860a24SChandler Carruth     // C  T  T  I  I  P  P  C  T  T  P  T   -+
2156*ef860a24SChandler Carruth     {  1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc      -+
2157*ef860a24SChandler Carruth     {  8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt        |
2158*ef860a24SChandler Carruth     {  8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt        |
2159*ef860a24SChandler Carruth     {  0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI      |
2160*ef860a24SChandler Carruth     {  0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI      |
2161*ef860a24SChandler Carruth     { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP      +- firstOp
2162*ef860a24SChandler Carruth     { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP      |
2163*ef860a24SChandler Carruth     { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc     |
2164*ef860a24SChandler Carruth     { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt       |
2165*ef860a24SChandler Carruth     {  1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt    |
2166*ef860a24SChandler Carruth     { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr    |
2167*ef860a24SChandler Carruth     {  5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast    -+
2168*ef860a24SChandler Carruth   };
2169*ef860a24SChandler Carruth 
2170*ef860a24SChandler Carruth   // If either of the casts are a bitcast from scalar to vector, disallow the
2171*ef860a24SChandler Carruth   // merging. However, bitcast of A->B->A are allowed.
2172*ef860a24SChandler Carruth   bool isFirstBitcast  = (firstOp == Instruction::BitCast);
2173*ef860a24SChandler Carruth   bool isSecondBitcast = (secondOp == Instruction::BitCast);
2174*ef860a24SChandler Carruth   bool chainedBitcast  = (SrcTy == DstTy && isFirstBitcast && isSecondBitcast);
2175*ef860a24SChandler Carruth 
2176*ef860a24SChandler Carruth   // Check if any of the bitcasts convert scalars<->vectors.
2177*ef860a24SChandler Carruth   if ((isFirstBitcast  && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2178*ef860a24SChandler Carruth       (isSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2179*ef860a24SChandler Carruth     // Unless we are bitcasing to the original type, disallow optimizations.
2180*ef860a24SChandler Carruth     if (!chainedBitcast) return 0;
2181*ef860a24SChandler Carruth 
2182*ef860a24SChandler Carruth   int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2183*ef860a24SChandler Carruth                             [secondOp-Instruction::CastOpsBegin];
2184*ef860a24SChandler Carruth   switch (ElimCase) {
2185*ef860a24SChandler Carruth     case 0:
2186*ef860a24SChandler Carruth       // categorically disallowed
2187*ef860a24SChandler Carruth       return 0;
2188*ef860a24SChandler Carruth     case 1:
2189*ef860a24SChandler Carruth       // allowed, use first cast's opcode
2190*ef860a24SChandler Carruth       return firstOp;
2191*ef860a24SChandler Carruth     case 2:
2192*ef860a24SChandler Carruth       // allowed, use second cast's opcode
2193*ef860a24SChandler Carruth       return secondOp;
2194*ef860a24SChandler Carruth     case 3:
2195*ef860a24SChandler Carruth       // no-op cast in second op implies firstOp as long as the DestTy
2196*ef860a24SChandler Carruth       // is integer and we are not converting between a vector and a
2197*ef860a24SChandler Carruth       // non vector type.
2198*ef860a24SChandler Carruth       if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
2199*ef860a24SChandler Carruth         return firstOp;
2200*ef860a24SChandler Carruth       return 0;
2201*ef860a24SChandler Carruth     case 4:
2202*ef860a24SChandler Carruth       // no-op cast in second op implies firstOp as long as the DestTy
2203*ef860a24SChandler Carruth       // is floating point.
2204*ef860a24SChandler Carruth       if (DstTy->isFloatingPointTy())
2205*ef860a24SChandler Carruth         return firstOp;
2206*ef860a24SChandler Carruth       return 0;
2207*ef860a24SChandler Carruth     case 5:
2208*ef860a24SChandler Carruth       // no-op cast in first op implies secondOp as long as the SrcTy
2209*ef860a24SChandler Carruth       // is an integer.
2210*ef860a24SChandler Carruth       if (SrcTy->isIntegerTy())
2211*ef860a24SChandler Carruth         return secondOp;
2212*ef860a24SChandler Carruth       return 0;
2213*ef860a24SChandler Carruth     case 6:
2214*ef860a24SChandler Carruth       // no-op cast in first op implies secondOp as long as the SrcTy
2215*ef860a24SChandler Carruth       // is a floating point.
2216*ef860a24SChandler Carruth       if (SrcTy->isFloatingPointTy())
2217*ef860a24SChandler Carruth         return secondOp;
2218*ef860a24SChandler Carruth       return 0;
2219*ef860a24SChandler Carruth     case 7: {
2220*ef860a24SChandler Carruth       // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
2221*ef860a24SChandler Carruth       if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
2222*ef860a24SChandler Carruth         return 0;
2223*ef860a24SChandler Carruth       unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
2224*ef860a24SChandler Carruth       unsigned MidSize = MidTy->getScalarSizeInBits();
2225*ef860a24SChandler Carruth       if (MidSize >= PtrSize)
2226*ef860a24SChandler Carruth         return Instruction::BitCast;
2227*ef860a24SChandler Carruth       return 0;
2228*ef860a24SChandler Carruth     }
2229*ef860a24SChandler Carruth     case 8: {
2230*ef860a24SChandler Carruth       // ext, trunc -> bitcast,    if the SrcTy and DstTy are same size
2231*ef860a24SChandler Carruth       // ext, trunc -> ext,        if sizeof(SrcTy) < sizeof(DstTy)
2232*ef860a24SChandler Carruth       // ext, trunc -> trunc,      if sizeof(SrcTy) > sizeof(DstTy)
2233*ef860a24SChandler Carruth       unsigned SrcSize = SrcTy->getScalarSizeInBits();
2234*ef860a24SChandler Carruth       unsigned DstSize = DstTy->getScalarSizeInBits();
2235*ef860a24SChandler Carruth       if (SrcSize == DstSize)
2236*ef860a24SChandler Carruth         return Instruction::BitCast;
2237*ef860a24SChandler Carruth       else if (SrcSize < DstSize)
2238*ef860a24SChandler Carruth         return firstOp;
2239*ef860a24SChandler Carruth       return secondOp;
2240*ef860a24SChandler Carruth     }
2241*ef860a24SChandler Carruth     case 9: // zext, sext -> zext, because sext can't sign extend after zext
2242*ef860a24SChandler Carruth       return Instruction::ZExt;
2243*ef860a24SChandler Carruth     case 10:
2244*ef860a24SChandler Carruth       // fpext followed by ftrunc is allowed if the bit size returned to is
2245*ef860a24SChandler Carruth       // the same as the original, in which case its just a bitcast
2246*ef860a24SChandler Carruth       if (SrcTy == DstTy)
2247*ef860a24SChandler Carruth         return Instruction::BitCast;
2248*ef860a24SChandler Carruth       return 0; // If the types are not the same we can't eliminate it.
2249*ef860a24SChandler Carruth     case 11:
2250*ef860a24SChandler Carruth       // bitcast followed by ptrtoint is allowed as long as the bitcast
2251*ef860a24SChandler Carruth       // is a pointer to pointer cast.
2252*ef860a24SChandler Carruth       if (SrcTy->isPointerTy() && MidTy->isPointerTy())
2253*ef860a24SChandler Carruth         return secondOp;
2254*ef860a24SChandler Carruth       return 0;
2255*ef860a24SChandler Carruth     case 12:
2256*ef860a24SChandler Carruth       // inttoptr, bitcast -> intptr  if bitcast is a ptr to ptr cast
2257*ef860a24SChandler Carruth       if (MidTy->isPointerTy() && DstTy->isPointerTy())
2258*ef860a24SChandler Carruth         return firstOp;
2259*ef860a24SChandler Carruth       return 0;
2260*ef860a24SChandler Carruth     case 13: {
2261*ef860a24SChandler Carruth       // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
2262*ef860a24SChandler Carruth       if (!MidIntPtrTy)
2263*ef860a24SChandler Carruth         return 0;
2264*ef860a24SChandler Carruth       unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
2265*ef860a24SChandler Carruth       unsigned SrcSize = SrcTy->getScalarSizeInBits();
2266*ef860a24SChandler Carruth       unsigned DstSize = DstTy->getScalarSizeInBits();
2267*ef860a24SChandler Carruth       if (SrcSize <= PtrSize && SrcSize == DstSize)
2268*ef860a24SChandler Carruth         return Instruction::BitCast;
2269*ef860a24SChandler Carruth       return 0;
2270*ef860a24SChandler Carruth     }
2271*ef860a24SChandler Carruth     case 99:
2272*ef860a24SChandler Carruth       // cast combination can't happen (error in input). This is for all cases
2273*ef860a24SChandler Carruth       // where the MidTy is not the same for the two cast instructions.
2274*ef860a24SChandler Carruth       llvm_unreachable("Invalid Cast Combination");
2275*ef860a24SChandler Carruth     default:
2276*ef860a24SChandler Carruth       llvm_unreachable("Error in CastResults table!!!");
2277*ef860a24SChandler Carruth   }
2278*ef860a24SChandler Carruth }
2279*ef860a24SChandler Carruth 
2280*ef860a24SChandler Carruth CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
2281*ef860a24SChandler Carruth   const Twine &Name, Instruction *InsertBefore) {
2282*ef860a24SChandler Carruth   assert(castIsValid(op, S, Ty) && "Invalid cast!");
2283*ef860a24SChandler Carruth   // Construct and return the appropriate CastInst subclass
2284*ef860a24SChandler Carruth   switch (op) {
2285*ef860a24SChandler Carruth     case Trunc:    return new TruncInst    (S, Ty, Name, InsertBefore);
2286*ef860a24SChandler Carruth     case ZExt:     return new ZExtInst     (S, Ty, Name, InsertBefore);
2287*ef860a24SChandler Carruth     case SExt:     return new SExtInst     (S, Ty, Name, InsertBefore);
2288*ef860a24SChandler Carruth     case FPTrunc:  return new FPTruncInst  (S, Ty, Name, InsertBefore);
2289*ef860a24SChandler Carruth     case FPExt:    return new FPExtInst    (S, Ty, Name, InsertBefore);
2290*ef860a24SChandler Carruth     case UIToFP:   return new UIToFPInst   (S, Ty, Name, InsertBefore);
2291*ef860a24SChandler Carruth     case SIToFP:   return new SIToFPInst   (S, Ty, Name, InsertBefore);
2292*ef860a24SChandler Carruth     case FPToUI:   return new FPToUIInst   (S, Ty, Name, InsertBefore);
2293*ef860a24SChandler Carruth     case FPToSI:   return new FPToSIInst   (S, Ty, Name, InsertBefore);
2294*ef860a24SChandler Carruth     case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2295*ef860a24SChandler Carruth     case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2296*ef860a24SChandler Carruth     case BitCast:  return new BitCastInst  (S, Ty, Name, InsertBefore);
2297*ef860a24SChandler Carruth     default: llvm_unreachable("Invalid opcode provided");
2298*ef860a24SChandler Carruth   }
2299*ef860a24SChandler Carruth }
2300*ef860a24SChandler Carruth 
2301*ef860a24SChandler Carruth CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
2302*ef860a24SChandler Carruth   const Twine &Name, BasicBlock *InsertAtEnd) {
2303*ef860a24SChandler Carruth   assert(castIsValid(op, S, Ty) && "Invalid cast!");
2304*ef860a24SChandler Carruth   // Construct and return the appropriate CastInst subclass
2305*ef860a24SChandler Carruth   switch (op) {
2306*ef860a24SChandler Carruth     case Trunc:    return new TruncInst    (S, Ty, Name, InsertAtEnd);
2307*ef860a24SChandler Carruth     case ZExt:     return new ZExtInst     (S, Ty, Name, InsertAtEnd);
2308*ef860a24SChandler Carruth     case SExt:     return new SExtInst     (S, Ty, Name, InsertAtEnd);
2309*ef860a24SChandler Carruth     case FPTrunc:  return new FPTruncInst  (S, Ty, Name, InsertAtEnd);
2310*ef860a24SChandler Carruth     case FPExt:    return new FPExtInst    (S, Ty, Name, InsertAtEnd);
2311*ef860a24SChandler Carruth     case UIToFP:   return new UIToFPInst   (S, Ty, Name, InsertAtEnd);
2312*ef860a24SChandler Carruth     case SIToFP:   return new SIToFPInst   (S, Ty, Name, InsertAtEnd);
2313*ef860a24SChandler Carruth     case FPToUI:   return new FPToUIInst   (S, Ty, Name, InsertAtEnd);
2314*ef860a24SChandler Carruth     case FPToSI:   return new FPToSIInst   (S, Ty, Name, InsertAtEnd);
2315*ef860a24SChandler Carruth     case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2316*ef860a24SChandler Carruth     case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2317*ef860a24SChandler Carruth     case BitCast:  return new BitCastInst  (S, Ty, Name, InsertAtEnd);
2318*ef860a24SChandler Carruth     default: llvm_unreachable("Invalid opcode provided");
2319*ef860a24SChandler Carruth   }
2320*ef860a24SChandler Carruth }
2321*ef860a24SChandler Carruth 
2322*ef860a24SChandler Carruth CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
2323*ef860a24SChandler Carruth                                         const Twine &Name,
2324*ef860a24SChandler Carruth                                         Instruction *InsertBefore) {
2325*ef860a24SChandler Carruth   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2326*ef860a24SChandler Carruth     return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2327*ef860a24SChandler Carruth   return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
2328*ef860a24SChandler Carruth }
2329*ef860a24SChandler Carruth 
2330*ef860a24SChandler Carruth CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
2331*ef860a24SChandler Carruth                                         const Twine &Name,
2332*ef860a24SChandler Carruth                                         BasicBlock *InsertAtEnd) {
2333*ef860a24SChandler Carruth   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2334*ef860a24SChandler Carruth     return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2335*ef860a24SChandler Carruth   return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
2336*ef860a24SChandler Carruth }
2337*ef860a24SChandler Carruth 
2338*ef860a24SChandler Carruth CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
2339*ef860a24SChandler Carruth                                         const Twine &Name,
2340*ef860a24SChandler Carruth                                         Instruction *InsertBefore) {
2341*ef860a24SChandler Carruth   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2342*ef860a24SChandler Carruth     return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2343*ef860a24SChandler Carruth   return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
2344*ef860a24SChandler Carruth }
2345*ef860a24SChandler Carruth 
2346*ef860a24SChandler Carruth CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
2347*ef860a24SChandler Carruth                                         const Twine &Name,
2348*ef860a24SChandler Carruth                                         BasicBlock *InsertAtEnd) {
2349*ef860a24SChandler Carruth   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2350*ef860a24SChandler Carruth     return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2351*ef860a24SChandler Carruth   return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
2352*ef860a24SChandler Carruth }
2353*ef860a24SChandler Carruth 
2354*ef860a24SChandler Carruth CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
2355*ef860a24SChandler Carruth                                          const Twine &Name,
2356*ef860a24SChandler Carruth                                          Instruction *InsertBefore) {
2357*ef860a24SChandler Carruth   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2358*ef860a24SChandler Carruth     return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2359*ef860a24SChandler Carruth   return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
2360*ef860a24SChandler Carruth }
2361*ef860a24SChandler Carruth 
2362*ef860a24SChandler Carruth CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
2363*ef860a24SChandler Carruth                                          const Twine &Name,
2364*ef860a24SChandler Carruth                                          BasicBlock *InsertAtEnd) {
2365*ef860a24SChandler Carruth   if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
2366*ef860a24SChandler Carruth     return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2367*ef860a24SChandler Carruth   return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
2368*ef860a24SChandler Carruth }
2369*ef860a24SChandler Carruth 
2370*ef860a24SChandler Carruth CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2371*ef860a24SChandler Carruth                                       const Twine &Name,
2372*ef860a24SChandler Carruth                                       BasicBlock *InsertAtEnd) {
2373*ef860a24SChandler Carruth   assert(S->getType()->isPointerTy() && "Invalid cast");
2374*ef860a24SChandler Carruth   assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2375*ef860a24SChandler Carruth          "Invalid cast");
2376*ef860a24SChandler Carruth 
2377*ef860a24SChandler Carruth   if (Ty->isIntegerTy())
2378*ef860a24SChandler Carruth     return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2379*ef860a24SChandler Carruth   return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2380*ef860a24SChandler Carruth }
2381*ef860a24SChandler Carruth 
2382*ef860a24SChandler Carruth /// @brief Create a BitCast or a PtrToInt cast instruction
2383*ef860a24SChandler Carruth CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2384*ef860a24SChandler Carruth                                       const Twine &Name,
2385*ef860a24SChandler Carruth                                       Instruction *InsertBefore) {
2386*ef860a24SChandler Carruth   assert(S->getType()->isPointerTy() && "Invalid cast");
2387*ef860a24SChandler Carruth   assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
2388*ef860a24SChandler Carruth          "Invalid cast");
2389*ef860a24SChandler Carruth 
2390*ef860a24SChandler Carruth   if (Ty->isIntegerTy())
2391*ef860a24SChandler Carruth     return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2392*ef860a24SChandler Carruth   return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2393*ef860a24SChandler Carruth }
2394*ef860a24SChandler Carruth 
2395*ef860a24SChandler Carruth CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
2396*ef860a24SChandler Carruth                                       bool isSigned, const Twine &Name,
2397*ef860a24SChandler Carruth                                       Instruction *InsertBefore) {
2398*ef860a24SChandler Carruth   assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2399*ef860a24SChandler Carruth          "Invalid integer cast");
2400*ef860a24SChandler Carruth   unsigned SrcBits = C->getType()->getScalarSizeInBits();
2401*ef860a24SChandler Carruth   unsigned DstBits = Ty->getScalarSizeInBits();
2402*ef860a24SChandler Carruth   Instruction::CastOps opcode =
2403*ef860a24SChandler Carruth     (SrcBits == DstBits ? Instruction::BitCast :
2404*ef860a24SChandler Carruth      (SrcBits > DstBits ? Instruction::Trunc :
2405*ef860a24SChandler Carruth       (isSigned ? Instruction::SExt : Instruction::ZExt)));
2406*ef860a24SChandler Carruth   return Create(opcode, C, Ty, Name, InsertBefore);
2407*ef860a24SChandler Carruth }
2408*ef860a24SChandler Carruth 
2409*ef860a24SChandler Carruth CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
2410*ef860a24SChandler Carruth                                       bool isSigned, const Twine &Name,
2411*ef860a24SChandler Carruth                                       BasicBlock *InsertAtEnd) {
2412*ef860a24SChandler Carruth   assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
2413*ef860a24SChandler Carruth          "Invalid cast");
2414*ef860a24SChandler Carruth   unsigned SrcBits = C->getType()->getScalarSizeInBits();
2415*ef860a24SChandler Carruth   unsigned DstBits = Ty->getScalarSizeInBits();
2416*ef860a24SChandler Carruth   Instruction::CastOps opcode =
2417*ef860a24SChandler Carruth     (SrcBits == DstBits ? Instruction::BitCast :
2418*ef860a24SChandler Carruth      (SrcBits > DstBits ? Instruction::Trunc :
2419*ef860a24SChandler Carruth       (isSigned ? Instruction::SExt : Instruction::ZExt)));
2420*ef860a24SChandler Carruth   return Create(opcode, C, Ty, Name, InsertAtEnd);
2421*ef860a24SChandler Carruth }
2422*ef860a24SChandler Carruth 
2423*ef860a24SChandler Carruth CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2424*ef860a24SChandler Carruth                                  const Twine &Name,
2425*ef860a24SChandler Carruth                                  Instruction *InsertBefore) {
2426*ef860a24SChandler Carruth   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2427*ef860a24SChandler Carruth          "Invalid cast");
2428*ef860a24SChandler Carruth   unsigned SrcBits = C->getType()->getScalarSizeInBits();
2429*ef860a24SChandler Carruth   unsigned DstBits = Ty->getScalarSizeInBits();
2430*ef860a24SChandler Carruth   Instruction::CastOps opcode =
2431*ef860a24SChandler Carruth     (SrcBits == DstBits ? Instruction::BitCast :
2432*ef860a24SChandler Carruth      (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2433*ef860a24SChandler Carruth   return Create(opcode, C, Ty, Name, InsertBefore);
2434*ef860a24SChandler Carruth }
2435*ef860a24SChandler Carruth 
2436*ef860a24SChandler Carruth CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
2437*ef860a24SChandler Carruth                                  const Twine &Name,
2438*ef860a24SChandler Carruth                                  BasicBlock *InsertAtEnd) {
2439*ef860a24SChandler Carruth   assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
2440*ef860a24SChandler Carruth          "Invalid cast");
2441*ef860a24SChandler Carruth   unsigned SrcBits = C->getType()->getScalarSizeInBits();
2442*ef860a24SChandler Carruth   unsigned DstBits = Ty->getScalarSizeInBits();
2443*ef860a24SChandler Carruth   Instruction::CastOps opcode =
2444*ef860a24SChandler Carruth     (SrcBits == DstBits ? Instruction::BitCast :
2445*ef860a24SChandler Carruth      (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
2446*ef860a24SChandler Carruth   return Create(opcode, C, Ty, Name, InsertAtEnd);
2447*ef860a24SChandler Carruth }
2448*ef860a24SChandler Carruth 
2449*ef860a24SChandler Carruth // Check whether it is valid to call getCastOpcode for these types.
2450*ef860a24SChandler Carruth // This routine must be kept in sync with getCastOpcode.
2451*ef860a24SChandler Carruth bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2452*ef860a24SChandler Carruth   if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2453*ef860a24SChandler Carruth     return false;
2454*ef860a24SChandler Carruth 
2455*ef860a24SChandler Carruth   if (SrcTy == DestTy)
2456*ef860a24SChandler Carruth     return true;
2457*ef860a24SChandler Carruth 
2458*ef860a24SChandler Carruth   if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2459*ef860a24SChandler Carruth     if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2460*ef860a24SChandler Carruth       if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2461*ef860a24SChandler Carruth         // An element by element cast.  Valid if casting the elements is valid.
2462*ef860a24SChandler Carruth         SrcTy = SrcVecTy->getElementType();
2463*ef860a24SChandler Carruth         DestTy = DestVecTy->getElementType();
2464*ef860a24SChandler Carruth       }
2465*ef860a24SChandler Carruth 
2466*ef860a24SChandler Carruth   // Get the bit sizes, we'll need these
2467*ef860a24SChandler Carruth   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr
2468*ef860a24SChandler Carruth   unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2469*ef860a24SChandler Carruth 
2470*ef860a24SChandler Carruth   // Run through the possibilities ...
2471*ef860a24SChandler Carruth   if (DestTy->isIntegerTy()) {               // Casting to integral
2472*ef860a24SChandler Carruth     if (SrcTy->isIntegerTy()) {                // Casting from integral
2473*ef860a24SChandler Carruth         return true;
2474*ef860a24SChandler Carruth     } else if (SrcTy->isFloatingPointTy()) {   // Casting from floating pt
2475*ef860a24SChandler Carruth       return true;
2476*ef860a24SChandler Carruth     } else if (SrcTy->isVectorTy()) {          // Casting from vector
2477*ef860a24SChandler Carruth       return DestBits == SrcBits;
2478*ef860a24SChandler Carruth     } else {                                   // Casting from something else
2479*ef860a24SChandler Carruth       return SrcTy->isPointerTy();
2480*ef860a24SChandler Carruth     }
2481*ef860a24SChandler Carruth   } else if (DestTy->isFloatingPointTy()) {  // Casting to floating pt
2482*ef860a24SChandler Carruth     if (SrcTy->isIntegerTy()) {                // Casting from integral
2483*ef860a24SChandler Carruth       return true;
2484*ef860a24SChandler Carruth     } else if (SrcTy->isFloatingPointTy()) {   // Casting from floating pt
2485*ef860a24SChandler Carruth       return true;
2486*ef860a24SChandler Carruth     } else if (SrcTy->isVectorTy()) {          // Casting from vector
2487*ef860a24SChandler Carruth       return DestBits == SrcBits;
2488*ef860a24SChandler Carruth     } else {                                   // Casting from something else
2489*ef860a24SChandler Carruth       return false;
2490*ef860a24SChandler Carruth     }
2491*ef860a24SChandler Carruth   } else if (DestTy->isVectorTy()) {         // Casting to vector
2492*ef860a24SChandler Carruth     return DestBits == SrcBits;
2493*ef860a24SChandler Carruth   } else if (DestTy->isPointerTy()) {        // Casting to pointer
2494*ef860a24SChandler Carruth     if (SrcTy->isPointerTy()) {                // Casting from pointer
2495*ef860a24SChandler Carruth       return true;
2496*ef860a24SChandler Carruth     } else if (SrcTy->isIntegerTy()) {         // Casting from integral
2497*ef860a24SChandler Carruth       return true;
2498*ef860a24SChandler Carruth     } else {                                   // Casting from something else
2499*ef860a24SChandler Carruth       return false;
2500*ef860a24SChandler Carruth     }
2501*ef860a24SChandler Carruth   } else if (DestTy->isX86_MMXTy()) {
2502*ef860a24SChandler Carruth     if (SrcTy->isVectorTy()) {
2503*ef860a24SChandler Carruth       return DestBits == SrcBits;       // 64-bit vector to MMX
2504*ef860a24SChandler Carruth     } else {
2505*ef860a24SChandler Carruth       return false;
2506*ef860a24SChandler Carruth     }
2507*ef860a24SChandler Carruth   } else {                                   // Casting to something else
2508*ef860a24SChandler Carruth     return false;
2509*ef860a24SChandler Carruth   }
2510*ef860a24SChandler Carruth }
2511*ef860a24SChandler Carruth 
2512*ef860a24SChandler Carruth // Provide a way to get a "cast" where the cast opcode is inferred from the
2513*ef860a24SChandler Carruth // types and size of the operand. This, basically, is a parallel of the
2514*ef860a24SChandler Carruth // logic in the castIsValid function below.  This axiom should hold:
2515*ef860a24SChandler Carruth //   castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2516*ef860a24SChandler Carruth // should not assert in castIsValid. In other words, this produces a "correct"
2517*ef860a24SChandler Carruth // casting opcode for the arguments passed to it.
2518*ef860a24SChandler Carruth // This routine must be kept in sync with isCastable.
2519*ef860a24SChandler Carruth Instruction::CastOps
2520*ef860a24SChandler Carruth CastInst::getCastOpcode(
2521*ef860a24SChandler Carruth   const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2522*ef860a24SChandler Carruth   Type *SrcTy = Src->getType();
2523*ef860a24SChandler Carruth 
2524*ef860a24SChandler Carruth   assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2525*ef860a24SChandler Carruth          "Only first class types are castable!");
2526*ef860a24SChandler Carruth 
2527*ef860a24SChandler Carruth   if (SrcTy == DestTy)
2528*ef860a24SChandler Carruth     return BitCast;
2529*ef860a24SChandler Carruth 
2530*ef860a24SChandler Carruth   if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2531*ef860a24SChandler Carruth     if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2532*ef860a24SChandler Carruth       if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2533*ef860a24SChandler Carruth         // An element by element cast.  Find the appropriate opcode based on the
2534*ef860a24SChandler Carruth         // element types.
2535*ef860a24SChandler Carruth         SrcTy = SrcVecTy->getElementType();
2536*ef860a24SChandler Carruth         DestTy = DestVecTy->getElementType();
2537*ef860a24SChandler Carruth       }
2538*ef860a24SChandler Carruth 
2539*ef860a24SChandler Carruth   // Get the bit sizes, we'll need these
2540*ef860a24SChandler Carruth   unsigned SrcBits = SrcTy->getPrimitiveSizeInBits();   // 0 for ptr
2541*ef860a24SChandler Carruth   unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2542*ef860a24SChandler Carruth 
2543*ef860a24SChandler Carruth   // Run through the possibilities ...
2544*ef860a24SChandler Carruth   if (DestTy->isIntegerTy()) {                      // Casting to integral
2545*ef860a24SChandler Carruth     if (SrcTy->isIntegerTy()) {                     // Casting from integral
2546*ef860a24SChandler Carruth       if (DestBits < SrcBits)
2547*ef860a24SChandler Carruth         return Trunc;                               // int -> smaller int
2548*ef860a24SChandler Carruth       else if (DestBits > SrcBits) {                // its an extension
2549*ef860a24SChandler Carruth         if (SrcIsSigned)
2550*ef860a24SChandler Carruth           return SExt;                              // signed -> SEXT
2551*ef860a24SChandler Carruth         else
2552*ef860a24SChandler Carruth           return ZExt;                              // unsigned -> ZEXT
2553*ef860a24SChandler Carruth       } else {
2554*ef860a24SChandler Carruth         return BitCast;                             // Same size, No-op cast
2555*ef860a24SChandler Carruth       }
2556*ef860a24SChandler Carruth     } else if (SrcTy->isFloatingPointTy()) {        // Casting from floating pt
2557*ef860a24SChandler Carruth       if (DestIsSigned)
2558*ef860a24SChandler Carruth         return FPToSI;                              // FP -> sint
2559*ef860a24SChandler Carruth       else
2560*ef860a24SChandler Carruth         return FPToUI;                              // FP -> uint
2561*ef860a24SChandler Carruth     } else if (SrcTy->isVectorTy()) {
2562*ef860a24SChandler Carruth       assert(DestBits == SrcBits &&
2563*ef860a24SChandler Carruth              "Casting vector to integer of different width");
2564*ef860a24SChandler Carruth       return BitCast;                             // Same size, no-op cast
2565*ef860a24SChandler Carruth     } else {
2566*ef860a24SChandler Carruth       assert(SrcTy->isPointerTy() &&
2567*ef860a24SChandler Carruth              "Casting from a value that is not first-class type");
2568*ef860a24SChandler Carruth       return PtrToInt;                              // ptr -> int
2569*ef860a24SChandler Carruth     }
2570*ef860a24SChandler Carruth   } else if (DestTy->isFloatingPointTy()) {         // Casting to floating pt
2571*ef860a24SChandler Carruth     if (SrcTy->isIntegerTy()) {                     // Casting from integral
2572*ef860a24SChandler Carruth       if (SrcIsSigned)
2573*ef860a24SChandler Carruth         return SIToFP;                              // sint -> FP
2574*ef860a24SChandler Carruth       else
2575*ef860a24SChandler Carruth         return UIToFP;                              // uint -> FP
2576*ef860a24SChandler Carruth     } else if (SrcTy->isFloatingPointTy()) {        // Casting from floating pt
2577*ef860a24SChandler Carruth       if (DestBits < SrcBits) {
2578*ef860a24SChandler Carruth         return FPTrunc;                             // FP -> smaller FP
2579*ef860a24SChandler Carruth       } else if (DestBits > SrcBits) {
2580*ef860a24SChandler Carruth         return FPExt;                               // FP -> larger FP
2581*ef860a24SChandler Carruth       } else  {
2582*ef860a24SChandler Carruth         return BitCast;                             // same size, no-op cast
2583*ef860a24SChandler Carruth       }
2584*ef860a24SChandler Carruth     } else if (SrcTy->isVectorTy()) {
2585*ef860a24SChandler Carruth       assert(DestBits == SrcBits &&
2586*ef860a24SChandler Carruth              "Casting vector to floating point of different width");
2587*ef860a24SChandler Carruth       return BitCast;                             // same size, no-op cast
2588*ef860a24SChandler Carruth     }
2589*ef860a24SChandler Carruth     llvm_unreachable("Casting pointer or non-first class to float");
2590*ef860a24SChandler Carruth   } else if (DestTy->isVectorTy()) {
2591*ef860a24SChandler Carruth     assert(DestBits == SrcBits &&
2592*ef860a24SChandler Carruth            "Illegal cast to vector (wrong type or size)");
2593*ef860a24SChandler Carruth     return BitCast;
2594*ef860a24SChandler Carruth   } else if (DestTy->isPointerTy()) {
2595*ef860a24SChandler Carruth     if (SrcTy->isPointerTy()) {
2596*ef860a24SChandler Carruth       return BitCast;                               // ptr -> ptr
2597*ef860a24SChandler Carruth     } else if (SrcTy->isIntegerTy()) {
2598*ef860a24SChandler Carruth       return IntToPtr;                              // int -> ptr
2599*ef860a24SChandler Carruth     }
2600*ef860a24SChandler Carruth     llvm_unreachable("Casting pointer to other than pointer or int");
2601*ef860a24SChandler Carruth   } else if (DestTy->isX86_MMXTy()) {
2602*ef860a24SChandler Carruth     if (SrcTy->isVectorTy()) {
2603*ef860a24SChandler Carruth       assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
2604*ef860a24SChandler Carruth       return BitCast;                               // 64-bit vector to MMX
2605*ef860a24SChandler Carruth     }
2606*ef860a24SChandler Carruth     llvm_unreachable("Illegal cast to X86_MMX");
2607*ef860a24SChandler Carruth   }
2608*ef860a24SChandler Carruth   llvm_unreachable("Casting to type that is not first-class");
2609*ef860a24SChandler Carruth }
2610*ef860a24SChandler Carruth 
2611*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
2612*ef860a24SChandler Carruth //                    CastInst SubClass Constructors
2613*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
2614*ef860a24SChandler Carruth 
2615*ef860a24SChandler Carruth /// Check that the construction parameters for a CastInst are correct. This
2616*ef860a24SChandler Carruth /// could be broken out into the separate constructors but it is useful to have
2617*ef860a24SChandler Carruth /// it in one place and to eliminate the redundant code for getting the sizes
2618*ef860a24SChandler Carruth /// of the types involved.
2619*ef860a24SChandler Carruth bool
2620*ef860a24SChandler Carruth CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
2621*ef860a24SChandler Carruth 
2622*ef860a24SChandler Carruth   // Check for type sanity on the arguments
2623*ef860a24SChandler Carruth   Type *SrcTy = S->getType();
2624*ef860a24SChandler Carruth   if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2625*ef860a24SChandler Carruth       SrcTy->isAggregateType() || DstTy->isAggregateType())
2626*ef860a24SChandler Carruth     return false;
2627*ef860a24SChandler Carruth 
2628*ef860a24SChandler Carruth   // Get the size of the types in bits, we'll need this later
2629*ef860a24SChandler Carruth   unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2630*ef860a24SChandler Carruth   unsigned DstBitSize = DstTy->getScalarSizeInBits();
2631*ef860a24SChandler Carruth 
2632*ef860a24SChandler Carruth   // If these are vector types, get the lengths of the vectors (using zero for
2633*ef860a24SChandler Carruth   // scalar types means that checking that vector lengths match also checks that
2634*ef860a24SChandler Carruth   // scalars are not being converted to vectors or vectors to scalars).
2635*ef860a24SChandler Carruth   unsigned SrcLength = SrcTy->isVectorTy() ?
2636*ef860a24SChandler Carruth     cast<VectorType>(SrcTy)->getNumElements() : 0;
2637*ef860a24SChandler Carruth   unsigned DstLength = DstTy->isVectorTy() ?
2638*ef860a24SChandler Carruth     cast<VectorType>(DstTy)->getNumElements() : 0;
2639*ef860a24SChandler Carruth 
2640*ef860a24SChandler Carruth   // Switch on the opcode provided
2641*ef860a24SChandler Carruth   switch (op) {
2642*ef860a24SChandler Carruth   default: return false; // This is an input error
2643*ef860a24SChandler Carruth   case Instruction::Trunc:
2644*ef860a24SChandler Carruth     return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2645*ef860a24SChandler Carruth       SrcLength == DstLength && SrcBitSize > DstBitSize;
2646*ef860a24SChandler Carruth   case Instruction::ZExt:
2647*ef860a24SChandler Carruth     return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2648*ef860a24SChandler Carruth       SrcLength == DstLength && SrcBitSize < DstBitSize;
2649*ef860a24SChandler Carruth   case Instruction::SExt:
2650*ef860a24SChandler Carruth     return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
2651*ef860a24SChandler Carruth       SrcLength == DstLength && SrcBitSize < DstBitSize;
2652*ef860a24SChandler Carruth   case Instruction::FPTrunc:
2653*ef860a24SChandler Carruth     return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2654*ef860a24SChandler Carruth       SrcLength == DstLength && SrcBitSize > DstBitSize;
2655*ef860a24SChandler Carruth   case Instruction::FPExt:
2656*ef860a24SChandler Carruth     return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
2657*ef860a24SChandler Carruth       SrcLength == DstLength && SrcBitSize < DstBitSize;
2658*ef860a24SChandler Carruth   case Instruction::UIToFP:
2659*ef860a24SChandler Carruth   case Instruction::SIToFP:
2660*ef860a24SChandler Carruth     return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
2661*ef860a24SChandler Carruth       SrcLength == DstLength;
2662*ef860a24SChandler Carruth   case Instruction::FPToUI:
2663*ef860a24SChandler Carruth   case Instruction::FPToSI:
2664*ef860a24SChandler Carruth     return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
2665*ef860a24SChandler Carruth       SrcLength == DstLength;
2666*ef860a24SChandler Carruth   case Instruction::PtrToInt:
2667*ef860a24SChandler Carruth     if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
2668*ef860a24SChandler Carruth       return false;
2669*ef860a24SChandler Carruth     if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2670*ef860a24SChandler Carruth       if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2671*ef860a24SChandler Carruth         return false;
2672*ef860a24SChandler Carruth     return SrcTy->getScalarType()->isPointerTy() &&
2673*ef860a24SChandler Carruth            DstTy->getScalarType()->isIntegerTy();
2674*ef860a24SChandler Carruth   case Instruction::IntToPtr:
2675*ef860a24SChandler Carruth     if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
2676*ef860a24SChandler Carruth       return false;
2677*ef860a24SChandler Carruth     if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
2678*ef860a24SChandler Carruth       if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
2679*ef860a24SChandler Carruth         return false;
2680*ef860a24SChandler Carruth     return SrcTy->getScalarType()->isIntegerTy() &&
2681*ef860a24SChandler Carruth            DstTy->getScalarType()->isPointerTy();
2682*ef860a24SChandler Carruth   case Instruction::BitCast:
2683*ef860a24SChandler Carruth     // BitCast implies a no-op cast of type only. No bits change.
2684*ef860a24SChandler Carruth     // However, you can't cast pointers to anything but pointers.
2685*ef860a24SChandler Carruth     if (SrcTy->isPointerTy() != DstTy->isPointerTy())
2686*ef860a24SChandler Carruth       return false;
2687*ef860a24SChandler Carruth 
2688*ef860a24SChandler Carruth     // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
2689*ef860a24SChandler Carruth     // these cases, the cast is okay if the source and destination bit widths
2690*ef860a24SChandler Carruth     // are identical.
2691*ef860a24SChandler Carruth     return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
2692*ef860a24SChandler Carruth   }
2693*ef860a24SChandler Carruth }
2694*ef860a24SChandler Carruth 
2695*ef860a24SChandler Carruth TruncInst::TruncInst(
2696*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2697*ef860a24SChandler Carruth ) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
2698*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2699*ef860a24SChandler Carruth }
2700*ef860a24SChandler Carruth 
2701*ef860a24SChandler Carruth TruncInst::TruncInst(
2702*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2703*ef860a24SChandler Carruth ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
2704*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
2705*ef860a24SChandler Carruth }
2706*ef860a24SChandler Carruth 
2707*ef860a24SChandler Carruth ZExtInst::ZExtInst(
2708*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2709*ef860a24SChandler Carruth )  : CastInst(Ty, ZExt, S, Name, InsertBefore) {
2710*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2711*ef860a24SChandler Carruth }
2712*ef860a24SChandler Carruth 
2713*ef860a24SChandler Carruth ZExtInst::ZExtInst(
2714*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2715*ef860a24SChandler Carruth )  : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
2716*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
2717*ef860a24SChandler Carruth }
2718*ef860a24SChandler Carruth SExtInst::SExtInst(
2719*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2720*ef860a24SChandler Carruth ) : CastInst(Ty, SExt, S, Name, InsertBefore) {
2721*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2722*ef860a24SChandler Carruth }
2723*ef860a24SChandler Carruth 
2724*ef860a24SChandler Carruth SExtInst::SExtInst(
2725*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2726*ef860a24SChandler Carruth )  : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
2727*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
2728*ef860a24SChandler Carruth }
2729*ef860a24SChandler Carruth 
2730*ef860a24SChandler Carruth FPTruncInst::FPTruncInst(
2731*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2732*ef860a24SChandler Carruth ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
2733*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2734*ef860a24SChandler Carruth }
2735*ef860a24SChandler Carruth 
2736*ef860a24SChandler Carruth FPTruncInst::FPTruncInst(
2737*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2738*ef860a24SChandler Carruth ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
2739*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
2740*ef860a24SChandler Carruth }
2741*ef860a24SChandler Carruth 
2742*ef860a24SChandler Carruth FPExtInst::FPExtInst(
2743*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2744*ef860a24SChandler Carruth ) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
2745*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2746*ef860a24SChandler Carruth }
2747*ef860a24SChandler Carruth 
2748*ef860a24SChandler Carruth FPExtInst::FPExtInst(
2749*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2750*ef860a24SChandler Carruth ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
2751*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
2752*ef860a24SChandler Carruth }
2753*ef860a24SChandler Carruth 
2754*ef860a24SChandler Carruth UIToFPInst::UIToFPInst(
2755*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2756*ef860a24SChandler Carruth ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
2757*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2758*ef860a24SChandler Carruth }
2759*ef860a24SChandler Carruth 
2760*ef860a24SChandler Carruth UIToFPInst::UIToFPInst(
2761*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2762*ef860a24SChandler Carruth ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
2763*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
2764*ef860a24SChandler Carruth }
2765*ef860a24SChandler Carruth 
2766*ef860a24SChandler Carruth SIToFPInst::SIToFPInst(
2767*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2768*ef860a24SChandler Carruth ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
2769*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2770*ef860a24SChandler Carruth }
2771*ef860a24SChandler Carruth 
2772*ef860a24SChandler Carruth SIToFPInst::SIToFPInst(
2773*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2774*ef860a24SChandler Carruth ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
2775*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
2776*ef860a24SChandler Carruth }
2777*ef860a24SChandler Carruth 
2778*ef860a24SChandler Carruth FPToUIInst::FPToUIInst(
2779*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2780*ef860a24SChandler Carruth ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
2781*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2782*ef860a24SChandler Carruth }
2783*ef860a24SChandler Carruth 
2784*ef860a24SChandler Carruth FPToUIInst::FPToUIInst(
2785*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2786*ef860a24SChandler Carruth ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
2787*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
2788*ef860a24SChandler Carruth }
2789*ef860a24SChandler Carruth 
2790*ef860a24SChandler Carruth FPToSIInst::FPToSIInst(
2791*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2792*ef860a24SChandler Carruth ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
2793*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2794*ef860a24SChandler Carruth }
2795*ef860a24SChandler Carruth 
2796*ef860a24SChandler Carruth FPToSIInst::FPToSIInst(
2797*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2798*ef860a24SChandler Carruth ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
2799*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
2800*ef860a24SChandler Carruth }
2801*ef860a24SChandler Carruth 
2802*ef860a24SChandler Carruth PtrToIntInst::PtrToIntInst(
2803*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2804*ef860a24SChandler Carruth ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
2805*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2806*ef860a24SChandler Carruth }
2807*ef860a24SChandler Carruth 
2808*ef860a24SChandler Carruth PtrToIntInst::PtrToIntInst(
2809*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2810*ef860a24SChandler Carruth ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
2811*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
2812*ef860a24SChandler Carruth }
2813*ef860a24SChandler Carruth 
2814*ef860a24SChandler Carruth IntToPtrInst::IntToPtrInst(
2815*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2816*ef860a24SChandler Carruth ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
2817*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2818*ef860a24SChandler Carruth }
2819*ef860a24SChandler Carruth 
2820*ef860a24SChandler Carruth IntToPtrInst::IntToPtrInst(
2821*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2822*ef860a24SChandler Carruth ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
2823*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
2824*ef860a24SChandler Carruth }
2825*ef860a24SChandler Carruth 
2826*ef860a24SChandler Carruth BitCastInst::BitCastInst(
2827*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
2828*ef860a24SChandler Carruth ) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
2829*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2830*ef860a24SChandler Carruth }
2831*ef860a24SChandler Carruth 
2832*ef860a24SChandler Carruth BitCastInst::BitCastInst(
2833*ef860a24SChandler Carruth   Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
2834*ef860a24SChandler Carruth ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
2835*ef860a24SChandler Carruth   assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
2836*ef860a24SChandler Carruth }
2837*ef860a24SChandler Carruth 
2838*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
2839*ef860a24SChandler Carruth //                               CmpInst Classes
2840*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
2841*ef860a24SChandler Carruth 
2842*ef860a24SChandler Carruth void CmpInst::anchor() {}
2843*ef860a24SChandler Carruth 
2844*ef860a24SChandler Carruth CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
2845*ef860a24SChandler Carruth                  Value *LHS, Value *RHS, const Twine &Name,
2846*ef860a24SChandler Carruth                  Instruction *InsertBefore)
2847*ef860a24SChandler Carruth   : Instruction(ty, op,
2848*ef860a24SChandler Carruth                 OperandTraits<CmpInst>::op_begin(this),
2849*ef860a24SChandler Carruth                 OperandTraits<CmpInst>::operands(this),
2850*ef860a24SChandler Carruth                 InsertBefore) {
2851*ef860a24SChandler Carruth     Op<0>() = LHS;
2852*ef860a24SChandler Carruth     Op<1>() = RHS;
2853*ef860a24SChandler Carruth   setPredicate((Predicate)predicate);
2854*ef860a24SChandler Carruth   setName(Name);
2855*ef860a24SChandler Carruth }
2856*ef860a24SChandler Carruth 
2857*ef860a24SChandler Carruth CmpInst::CmpInst(Type *ty, OtherOps op, unsigned short predicate,
2858*ef860a24SChandler Carruth                  Value *LHS, Value *RHS, const Twine &Name,
2859*ef860a24SChandler Carruth                  BasicBlock *InsertAtEnd)
2860*ef860a24SChandler Carruth   : Instruction(ty, op,
2861*ef860a24SChandler Carruth                 OperandTraits<CmpInst>::op_begin(this),
2862*ef860a24SChandler Carruth                 OperandTraits<CmpInst>::operands(this),
2863*ef860a24SChandler Carruth                 InsertAtEnd) {
2864*ef860a24SChandler Carruth   Op<0>() = LHS;
2865*ef860a24SChandler Carruth   Op<1>() = RHS;
2866*ef860a24SChandler Carruth   setPredicate((Predicate)predicate);
2867*ef860a24SChandler Carruth   setName(Name);
2868*ef860a24SChandler Carruth }
2869*ef860a24SChandler Carruth 
2870*ef860a24SChandler Carruth CmpInst *
2871*ef860a24SChandler Carruth CmpInst::Create(OtherOps Op, unsigned short predicate,
2872*ef860a24SChandler Carruth                 Value *S1, Value *S2,
2873*ef860a24SChandler Carruth                 const Twine &Name, Instruction *InsertBefore) {
2874*ef860a24SChandler Carruth   if (Op == Instruction::ICmp) {
2875*ef860a24SChandler Carruth     if (InsertBefore)
2876*ef860a24SChandler Carruth       return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2877*ef860a24SChandler Carruth                           S1, S2, Name);
2878*ef860a24SChandler Carruth     else
2879*ef860a24SChandler Carruth       return new ICmpInst(CmpInst::Predicate(predicate),
2880*ef860a24SChandler Carruth                           S1, S2, Name);
2881*ef860a24SChandler Carruth   }
2882*ef860a24SChandler Carruth 
2883*ef860a24SChandler Carruth   if (InsertBefore)
2884*ef860a24SChandler Carruth     return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2885*ef860a24SChandler Carruth                         S1, S2, Name);
2886*ef860a24SChandler Carruth   else
2887*ef860a24SChandler Carruth     return new FCmpInst(CmpInst::Predicate(predicate),
2888*ef860a24SChandler Carruth                         S1, S2, Name);
2889*ef860a24SChandler Carruth }
2890*ef860a24SChandler Carruth 
2891*ef860a24SChandler Carruth CmpInst *
2892*ef860a24SChandler Carruth CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2893*ef860a24SChandler Carruth                 const Twine &Name, BasicBlock *InsertAtEnd) {
2894*ef860a24SChandler Carruth   if (Op == Instruction::ICmp) {
2895*ef860a24SChandler Carruth     return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2896*ef860a24SChandler Carruth                         S1, S2, Name);
2897*ef860a24SChandler Carruth   }
2898*ef860a24SChandler Carruth   return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2899*ef860a24SChandler Carruth                       S1, S2, Name);
2900*ef860a24SChandler Carruth }
2901*ef860a24SChandler Carruth 
2902*ef860a24SChandler Carruth void CmpInst::swapOperands() {
2903*ef860a24SChandler Carruth   if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2904*ef860a24SChandler Carruth     IC->swapOperands();
2905*ef860a24SChandler Carruth   else
2906*ef860a24SChandler Carruth     cast<FCmpInst>(this)->swapOperands();
2907*ef860a24SChandler Carruth }
2908*ef860a24SChandler Carruth 
2909*ef860a24SChandler Carruth bool CmpInst::isCommutative() const {
2910*ef860a24SChandler Carruth   if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
2911*ef860a24SChandler Carruth     return IC->isCommutative();
2912*ef860a24SChandler Carruth   return cast<FCmpInst>(this)->isCommutative();
2913*ef860a24SChandler Carruth }
2914*ef860a24SChandler Carruth 
2915*ef860a24SChandler Carruth bool CmpInst::isEquality() const {
2916*ef860a24SChandler Carruth   if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
2917*ef860a24SChandler Carruth     return IC->isEquality();
2918*ef860a24SChandler Carruth   return cast<FCmpInst>(this)->isEquality();
2919*ef860a24SChandler Carruth }
2920*ef860a24SChandler Carruth 
2921*ef860a24SChandler Carruth 
2922*ef860a24SChandler Carruth CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
2923*ef860a24SChandler Carruth   switch (pred) {
2924*ef860a24SChandler Carruth     default: llvm_unreachable("Unknown cmp predicate!");
2925*ef860a24SChandler Carruth     case ICMP_EQ: return ICMP_NE;
2926*ef860a24SChandler Carruth     case ICMP_NE: return ICMP_EQ;
2927*ef860a24SChandler Carruth     case ICMP_UGT: return ICMP_ULE;
2928*ef860a24SChandler Carruth     case ICMP_ULT: return ICMP_UGE;
2929*ef860a24SChandler Carruth     case ICMP_UGE: return ICMP_ULT;
2930*ef860a24SChandler Carruth     case ICMP_ULE: return ICMP_UGT;
2931*ef860a24SChandler Carruth     case ICMP_SGT: return ICMP_SLE;
2932*ef860a24SChandler Carruth     case ICMP_SLT: return ICMP_SGE;
2933*ef860a24SChandler Carruth     case ICMP_SGE: return ICMP_SLT;
2934*ef860a24SChandler Carruth     case ICMP_SLE: return ICMP_SGT;
2935*ef860a24SChandler Carruth 
2936*ef860a24SChandler Carruth     case FCMP_OEQ: return FCMP_UNE;
2937*ef860a24SChandler Carruth     case FCMP_ONE: return FCMP_UEQ;
2938*ef860a24SChandler Carruth     case FCMP_OGT: return FCMP_ULE;
2939*ef860a24SChandler Carruth     case FCMP_OLT: return FCMP_UGE;
2940*ef860a24SChandler Carruth     case FCMP_OGE: return FCMP_ULT;
2941*ef860a24SChandler Carruth     case FCMP_OLE: return FCMP_UGT;
2942*ef860a24SChandler Carruth     case FCMP_UEQ: return FCMP_ONE;
2943*ef860a24SChandler Carruth     case FCMP_UNE: return FCMP_OEQ;
2944*ef860a24SChandler Carruth     case FCMP_UGT: return FCMP_OLE;
2945*ef860a24SChandler Carruth     case FCMP_ULT: return FCMP_OGE;
2946*ef860a24SChandler Carruth     case FCMP_UGE: return FCMP_OLT;
2947*ef860a24SChandler Carruth     case FCMP_ULE: return FCMP_OGT;
2948*ef860a24SChandler Carruth     case FCMP_ORD: return FCMP_UNO;
2949*ef860a24SChandler Carruth     case FCMP_UNO: return FCMP_ORD;
2950*ef860a24SChandler Carruth     case FCMP_TRUE: return FCMP_FALSE;
2951*ef860a24SChandler Carruth     case FCMP_FALSE: return FCMP_TRUE;
2952*ef860a24SChandler Carruth   }
2953*ef860a24SChandler Carruth }
2954*ef860a24SChandler Carruth 
2955*ef860a24SChandler Carruth ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2956*ef860a24SChandler Carruth   switch (pred) {
2957*ef860a24SChandler Carruth     default: llvm_unreachable("Unknown icmp predicate!");
2958*ef860a24SChandler Carruth     case ICMP_EQ: case ICMP_NE:
2959*ef860a24SChandler Carruth     case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2960*ef860a24SChandler Carruth        return pred;
2961*ef860a24SChandler Carruth     case ICMP_UGT: return ICMP_SGT;
2962*ef860a24SChandler Carruth     case ICMP_ULT: return ICMP_SLT;
2963*ef860a24SChandler Carruth     case ICMP_UGE: return ICMP_SGE;
2964*ef860a24SChandler Carruth     case ICMP_ULE: return ICMP_SLE;
2965*ef860a24SChandler Carruth   }
2966*ef860a24SChandler Carruth }
2967*ef860a24SChandler Carruth 
2968*ef860a24SChandler Carruth ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2969*ef860a24SChandler Carruth   switch (pred) {
2970*ef860a24SChandler Carruth     default: llvm_unreachable("Unknown icmp predicate!");
2971*ef860a24SChandler Carruth     case ICMP_EQ: case ICMP_NE:
2972*ef860a24SChandler Carruth     case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2973*ef860a24SChandler Carruth        return pred;
2974*ef860a24SChandler Carruth     case ICMP_SGT: return ICMP_UGT;
2975*ef860a24SChandler Carruth     case ICMP_SLT: return ICMP_ULT;
2976*ef860a24SChandler Carruth     case ICMP_SGE: return ICMP_UGE;
2977*ef860a24SChandler Carruth     case ICMP_SLE: return ICMP_ULE;
2978*ef860a24SChandler Carruth   }
2979*ef860a24SChandler Carruth }
2980*ef860a24SChandler Carruth 
2981*ef860a24SChandler Carruth /// Initialize a set of values that all satisfy the condition with C.
2982*ef860a24SChandler Carruth ///
2983*ef860a24SChandler Carruth ConstantRange
2984*ef860a24SChandler Carruth ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2985*ef860a24SChandler Carruth   APInt Lower(C);
2986*ef860a24SChandler Carruth   APInt Upper(C);
2987*ef860a24SChandler Carruth   uint32_t BitWidth = C.getBitWidth();
2988*ef860a24SChandler Carruth   switch (pred) {
2989*ef860a24SChandler Carruth   default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
2990*ef860a24SChandler Carruth   case ICmpInst::ICMP_EQ: Upper++; break;
2991*ef860a24SChandler Carruth   case ICmpInst::ICMP_NE: Lower++; break;
2992*ef860a24SChandler Carruth   case ICmpInst::ICMP_ULT:
2993*ef860a24SChandler Carruth     Lower = APInt::getMinValue(BitWidth);
2994*ef860a24SChandler Carruth     // Check for an empty-set condition.
2995*ef860a24SChandler Carruth     if (Lower == Upper)
2996*ef860a24SChandler Carruth       return ConstantRange(BitWidth, /*isFullSet=*/false);
2997*ef860a24SChandler Carruth     break;
2998*ef860a24SChandler Carruth   case ICmpInst::ICMP_SLT:
2999*ef860a24SChandler Carruth     Lower = APInt::getSignedMinValue(BitWidth);
3000*ef860a24SChandler Carruth     // Check for an empty-set condition.
3001*ef860a24SChandler Carruth     if (Lower == Upper)
3002*ef860a24SChandler Carruth       return ConstantRange(BitWidth, /*isFullSet=*/false);
3003*ef860a24SChandler Carruth     break;
3004*ef860a24SChandler Carruth   case ICmpInst::ICMP_UGT:
3005*ef860a24SChandler Carruth     Lower++; Upper = APInt::getMinValue(BitWidth);        // Min = Next(Max)
3006*ef860a24SChandler Carruth     // Check for an empty-set condition.
3007*ef860a24SChandler Carruth     if (Lower == Upper)
3008*ef860a24SChandler Carruth       return ConstantRange(BitWidth, /*isFullSet=*/false);
3009*ef860a24SChandler Carruth     break;
3010*ef860a24SChandler Carruth   case ICmpInst::ICMP_SGT:
3011*ef860a24SChandler Carruth     Lower++; Upper = APInt::getSignedMinValue(BitWidth);  // Min = Next(Max)
3012*ef860a24SChandler Carruth     // Check for an empty-set condition.
3013*ef860a24SChandler Carruth     if (Lower == Upper)
3014*ef860a24SChandler Carruth       return ConstantRange(BitWidth, /*isFullSet=*/false);
3015*ef860a24SChandler Carruth     break;
3016*ef860a24SChandler Carruth   case ICmpInst::ICMP_ULE:
3017*ef860a24SChandler Carruth     Lower = APInt::getMinValue(BitWidth); Upper++;
3018*ef860a24SChandler Carruth     // Check for a full-set condition.
3019*ef860a24SChandler Carruth     if (Lower == Upper)
3020*ef860a24SChandler Carruth       return ConstantRange(BitWidth, /*isFullSet=*/true);
3021*ef860a24SChandler Carruth     break;
3022*ef860a24SChandler Carruth   case ICmpInst::ICMP_SLE:
3023*ef860a24SChandler Carruth     Lower = APInt::getSignedMinValue(BitWidth); Upper++;
3024*ef860a24SChandler Carruth     // Check for a full-set condition.
3025*ef860a24SChandler Carruth     if (Lower == Upper)
3026*ef860a24SChandler Carruth       return ConstantRange(BitWidth, /*isFullSet=*/true);
3027*ef860a24SChandler Carruth     break;
3028*ef860a24SChandler Carruth   case ICmpInst::ICMP_UGE:
3029*ef860a24SChandler Carruth     Upper = APInt::getMinValue(BitWidth);        // Min = Next(Max)
3030*ef860a24SChandler Carruth     // Check for a full-set condition.
3031*ef860a24SChandler Carruth     if (Lower == Upper)
3032*ef860a24SChandler Carruth       return ConstantRange(BitWidth, /*isFullSet=*/true);
3033*ef860a24SChandler Carruth     break;
3034*ef860a24SChandler Carruth   case ICmpInst::ICMP_SGE:
3035*ef860a24SChandler Carruth     Upper = APInt::getSignedMinValue(BitWidth);  // Min = Next(Max)
3036*ef860a24SChandler Carruth     // Check for a full-set condition.
3037*ef860a24SChandler Carruth     if (Lower == Upper)
3038*ef860a24SChandler Carruth       return ConstantRange(BitWidth, /*isFullSet=*/true);
3039*ef860a24SChandler Carruth     break;
3040*ef860a24SChandler Carruth   }
3041*ef860a24SChandler Carruth   return ConstantRange(Lower, Upper);
3042*ef860a24SChandler Carruth }
3043*ef860a24SChandler Carruth 
3044*ef860a24SChandler Carruth CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
3045*ef860a24SChandler Carruth   switch (pred) {
3046*ef860a24SChandler Carruth     default: llvm_unreachable("Unknown cmp predicate!");
3047*ef860a24SChandler Carruth     case ICMP_EQ: case ICMP_NE:
3048*ef860a24SChandler Carruth       return pred;
3049*ef860a24SChandler Carruth     case ICMP_SGT: return ICMP_SLT;
3050*ef860a24SChandler Carruth     case ICMP_SLT: return ICMP_SGT;
3051*ef860a24SChandler Carruth     case ICMP_SGE: return ICMP_SLE;
3052*ef860a24SChandler Carruth     case ICMP_SLE: return ICMP_SGE;
3053*ef860a24SChandler Carruth     case ICMP_UGT: return ICMP_ULT;
3054*ef860a24SChandler Carruth     case ICMP_ULT: return ICMP_UGT;
3055*ef860a24SChandler Carruth     case ICMP_UGE: return ICMP_ULE;
3056*ef860a24SChandler Carruth     case ICMP_ULE: return ICMP_UGE;
3057*ef860a24SChandler Carruth 
3058*ef860a24SChandler Carruth     case FCMP_FALSE: case FCMP_TRUE:
3059*ef860a24SChandler Carruth     case FCMP_OEQ: case FCMP_ONE:
3060*ef860a24SChandler Carruth     case FCMP_UEQ: case FCMP_UNE:
3061*ef860a24SChandler Carruth     case FCMP_ORD: case FCMP_UNO:
3062*ef860a24SChandler Carruth       return pred;
3063*ef860a24SChandler Carruth     case FCMP_OGT: return FCMP_OLT;
3064*ef860a24SChandler Carruth     case FCMP_OLT: return FCMP_OGT;
3065*ef860a24SChandler Carruth     case FCMP_OGE: return FCMP_OLE;
3066*ef860a24SChandler Carruth     case FCMP_OLE: return FCMP_OGE;
3067*ef860a24SChandler Carruth     case FCMP_UGT: return FCMP_ULT;
3068*ef860a24SChandler Carruth     case FCMP_ULT: return FCMP_UGT;
3069*ef860a24SChandler Carruth     case FCMP_UGE: return FCMP_ULE;
3070*ef860a24SChandler Carruth     case FCMP_ULE: return FCMP_UGE;
3071*ef860a24SChandler Carruth   }
3072*ef860a24SChandler Carruth }
3073*ef860a24SChandler Carruth 
3074*ef860a24SChandler Carruth bool CmpInst::isUnsigned(unsigned short predicate) {
3075*ef860a24SChandler Carruth   switch (predicate) {
3076*ef860a24SChandler Carruth     default: return false;
3077*ef860a24SChandler Carruth     case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3078*ef860a24SChandler Carruth     case ICmpInst::ICMP_UGE: return true;
3079*ef860a24SChandler Carruth   }
3080*ef860a24SChandler Carruth }
3081*ef860a24SChandler Carruth 
3082*ef860a24SChandler Carruth bool CmpInst::isSigned(unsigned short predicate) {
3083*ef860a24SChandler Carruth   switch (predicate) {
3084*ef860a24SChandler Carruth     default: return false;
3085*ef860a24SChandler Carruth     case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3086*ef860a24SChandler Carruth     case ICmpInst::ICMP_SGE: return true;
3087*ef860a24SChandler Carruth   }
3088*ef860a24SChandler Carruth }
3089*ef860a24SChandler Carruth 
3090*ef860a24SChandler Carruth bool CmpInst::isOrdered(unsigned short predicate) {
3091*ef860a24SChandler Carruth   switch (predicate) {
3092*ef860a24SChandler Carruth     default: return false;
3093*ef860a24SChandler Carruth     case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3094*ef860a24SChandler Carruth     case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3095*ef860a24SChandler Carruth     case FCmpInst::FCMP_ORD: return true;
3096*ef860a24SChandler Carruth   }
3097*ef860a24SChandler Carruth }
3098*ef860a24SChandler Carruth 
3099*ef860a24SChandler Carruth bool CmpInst::isUnordered(unsigned short predicate) {
3100*ef860a24SChandler Carruth   switch (predicate) {
3101*ef860a24SChandler Carruth     default: return false;
3102*ef860a24SChandler Carruth     case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3103*ef860a24SChandler Carruth     case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3104*ef860a24SChandler Carruth     case FCmpInst::FCMP_UNO: return true;
3105*ef860a24SChandler Carruth   }
3106*ef860a24SChandler Carruth }
3107*ef860a24SChandler Carruth 
3108*ef860a24SChandler Carruth bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
3109*ef860a24SChandler Carruth   switch(predicate) {
3110*ef860a24SChandler Carruth     default: return false;
3111*ef860a24SChandler Carruth     case ICMP_EQ:   case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3112*ef860a24SChandler Carruth     case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3113*ef860a24SChandler Carruth   }
3114*ef860a24SChandler Carruth }
3115*ef860a24SChandler Carruth 
3116*ef860a24SChandler Carruth bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
3117*ef860a24SChandler Carruth   switch(predicate) {
3118*ef860a24SChandler Carruth   case ICMP_NE:    case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3119*ef860a24SChandler Carruth   case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3120*ef860a24SChandler Carruth   default: return false;
3121*ef860a24SChandler Carruth   }
3122*ef860a24SChandler Carruth }
3123*ef860a24SChandler Carruth 
3124*ef860a24SChandler Carruth 
3125*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
3126*ef860a24SChandler Carruth //                        SwitchInst Implementation
3127*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
3128*ef860a24SChandler Carruth 
3129*ef860a24SChandler Carruth void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3130*ef860a24SChandler Carruth   assert(Value && Default && NumReserved);
3131*ef860a24SChandler Carruth   ReservedSpace = NumReserved;
3132*ef860a24SChandler Carruth   NumOperands = 2;
3133*ef860a24SChandler Carruth   OperandList = allocHungoffUses(ReservedSpace);
3134*ef860a24SChandler Carruth 
3135*ef860a24SChandler Carruth   OperandList[0] = Value;
3136*ef860a24SChandler Carruth   OperandList[1] = Default;
3137*ef860a24SChandler Carruth }
3138*ef860a24SChandler Carruth 
3139*ef860a24SChandler Carruth /// SwitchInst ctor - Create a new switch instruction, specifying a value to
3140*ef860a24SChandler Carruth /// switch on and a default destination.  The number of additional cases can
3141*ef860a24SChandler Carruth /// be specified here to make memory allocation more efficient.  This
3142*ef860a24SChandler Carruth /// constructor can also autoinsert before another instruction.
3143*ef860a24SChandler Carruth SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3144*ef860a24SChandler Carruth                        Instruction *InsertBefore)
3145*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3146*ef860a24SChandler Carruth                    0, 0, InsertBefore) {
3147*ef860a24SChandler Carruth   init(Value, Default, 2+NumCases*2);
3148*ef860a24SChandler Carruth }
3149*ef860a24SChandler Carruth 
3150*ef860a24SChandler Carruth /// SwitchInst ctor - Create a new switch instruction, specifying a value to
3151*ef860a24SChandler Carruth /// switch on and a default destination.  The number of additional cases can
3152*ef860a24SChandler Carruth /// be specified here to make memory allocation more efficient.  This
3153*ef860a24SChandler Carruth /// constructor also autoinserts at the end of the specified BasicBlock.
3154*ef860a24SChandler Carruth SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3155*ef860a24SChandler Carruth                        BasicBlock *InsertAtEnd)
3156*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
3157*ef860a24SChandler Carruth                    0, 0, InsertAtEnd) {
3158*ef860a24SChandler Carruth   init(Value, Default, 2+NumCases*2);
3159*ef860a24SChandler Carruth }
3160*ef860a24SChandler Carruth 
3161*ef860a24SChandler Carruth SwitchInst::SwitchInst(const SwitchInst &SI)
3162*ef860a24SChandler Carruth   : TerminatorInst(SI.getType(), Instruction::Switch, 0, 0) {
3163*ef860a24SChandler Carruth   init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
3164*ef860a24SChandler Carruth   NumOperands = SI.getNumOperands();
3165*ef860a24SChandler Carruth   Use *OL = OperandList, *InOL = SI.OperandList;
3166*ef860a24SChandler Carruth   for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
3167*ef860a24SChandler Carruth     OL[i] = InOL[i];
3168*ef860a24SChandler Carruth     OL[i+1] = InOL[i+1];
3169*ef860a24SChandler Carruth   }
3170*ef860a24SChandler Carruth   TheSubsets = SI.TheSubsets;
3171*ef860a24SChandler Carruth   SubclassOptionalData = SI.SubclassOptionalData;
3172*ef860a24SChandler Carruth }
3173*ef860a24SChandler Carruth 
3174*ef860a24SChandler Carruth SwitchInst::~SwitchInst() {
3175*ef860a24SChandler Carruth   dropHungoffUses();
3176*ef860a24SChandler Carruth }
3177*ef860a24SChandler Carruth 
3178*ef860a24SChandler Carruth 
3179*ef860a24SChandler Carruth /// addCase - Add an entry to the switch instruction...
3180*ef860a24SChandler Carruth ///
3181*ef860a24SChandler Carruth void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
3182*ef860a24SChandler Carruth   IntegersSubsetToBB Mapping;
3183*ef860a24SChandler Carruth 
3184*ef860a24SChandler Carruth   // FIXME: Currently we work with ConstantInt based cases.
3185*ef860a24SChandler Carruth   // So inititalize IntItem container directly from ConstantInt.
3186*ef860a24SChandler Carruth   Mapping.add(IntItem::fromConstantInt(OnVal));
3187*ef860a24SChandler Carruth   IntegersSubset CaseRanges = Mapping.getCase();
3188*ef860a24SChandler Carruth   addCase(CaseRanges, Dest);
3189*ef860a24SChandler Carruth }
3190*ef860a24SChandler Carruth 
3191*ef860a24SChandler Carruth void SwitchInst::addCase(IntegersSubset& OnVal, BasicBlock *Dest) {
3192*ef860a24SChandler Carruth   unsigned NewCaseIdx = getNumCases();
3193*ef860a24SChandler Carruth   unsigned OpNo = NumOperands;
3194*ef860a24SChandler Carruth   if (OpNo+2 > ReservedSpace)
3195*ef860a24SChandler Carruth     growOperands();  // Get more space!
3196*ef860a24SChandler Carruth   // Initialize some new operands.
3197*ef860a24SChandler Carruth   assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
3198*ef860a24SChandler Carruth   NumOperands = OpNo+2;
3199*ef860a24SChandler Carruth 
3200*ef860a24SChandler Carruth   SubsetsIt TheSubsetsIt = TheSubsets.insert(TheSubsets.end(), OnVal);
3201*ef860a24SChandler Carruth 
3202*ef860a24SChandler Carruth   CaseIt Case(this, NewCaseIdx, TheSubsetsIt);
3203*ef860a24SChandler Carruth   Case.updateCaseValueOperand(OnVal);
3204*ef860a24SChandler Carruth   Case.setSuccessor(Dest);
3205*ef860a24SChandler Carruth }
3206*ef860a24SChandler Carruth 
3207*ef860a24SChandler Carruth /// removeCase - This method removes the specified case and its successor
3208*ef860a24SChandler Carruth /// from the switch instruction.
3209*ef860a24SChandler Carruth void SwitchInst::removeCase(CaseIt& i) {
3210*ef860a24SChandler Carruth   unsigned idx = i.getCaseIndex();
3211*ef860a24SChandler Carruth 
3212*ef860a24SChandler Carruth   assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
3213*ef860a24SChandler Carruth 
3214*ef860a24SChandler Carruth   unsigned NumOps = getNumOperands();
3215*ef860a24SChandler Carruth   Use *OL = OperandList;
3216*ef860a24SChandler Carruth 
3217*ef860a24SChandler Carruth   // Overwrite this case with the end of the list.
3218*ef860a24SChandler Carruth   if (2 + (idx + 1) * 2 != NumOps) {
3219*ef860a24SChandler Carruth     OL[2 + idx * 2] = OL[NumOps - 2];
3220*ef860a24SChandler Carruth     OL[2 + idx * 2 + 1] = OL[NumOps - 1];
3221*ef860a24SChandler Carruth   }
3222*ef860a24SChandler Carruth 
3223*ef860a24SChandler Carruth   // Nuke the last value.
3224*ef860a24SChandler Carruth   OL[NumOps-2].set(0);
3225*ef860a24SChandler Carruth   OL[NumOps-2+1].set(0);
3226*ef860a24SChandler Carruth 
3227*ef860a24SChandler Carruth   // Do the same with TheCases collection:
3228*ef860a24SChandler Carruth   if (i.SubsetIt != --TheSubsets.end()) {
3229*ef860a24SChandler Carruth     *i.SubsetIt = TheSubsets.back();
3230*ef860a24SChandler Carruth     TheSubsets.pop_back();
3231*ef860a24SChandler Carruth   } else {
3232*ef860a24SChandler Carruth     TheSubsets.pop_back();
3233*ef860a24SChandler Carruth     i.SubsetIt = TheSubsets.end();
3234*ef860a24SChandler Carruth   }
3235*ef860a24SChandler Carruth 
3236*ef860a24SChandler Carruth   NumOperands = NumOps-2;
3237*ef860a24SChandler Carruth }
3238*ef860a24SChandler Carruth 
3239*ef860a24SChandler Carruth /// growOperands - grow operands - This grows the operand list in response
3240*ef860a24SChandler Carruth /// to a push_back style of operation.  This grows the number of ops by 3 times.
3241*ef860a24SChandler Carruth ///
3242*ef860a24SChandler Carruth void SwitchInst::growOperands() {
3243*ef860a24SChandler Carruth   unsigned e = getNumOperands();
3244*ef860a24SChandler Carruth   unsigned NumOps = e*3;
3245*ef860a24SChandler Carruth 
3246*ef860a24SChandler Carruth   ReservedSpace = NumOps;
3247*ef860a24SChandler Carruth   Use *NewOps = allocHungoffUses(NumOps);
3248*ef860a24SChandler Carruth   Use *OldOps = OperandList;
3249*ef860a24SChandler Carruth   for (unsigned i = 0; i != e; ++i) {
3250*ef860a24SChandler Carruth       NewOps[i] = OldOps[i];
3251*ef860a24SChandler Carruth   }
3252*ef860a24SChandler Carruth   OperandList = NewOps;
3253*ef860a24SChandler Carruth   Use::zap(OldOps, OldOps + e, true);
3254*ef860a24SChandler Carruth }
3255*ef860a24SChandler Carruth 
3256*ef860a24SChandler Carruth 
3257*ef860a24SChandler Carruth BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3258*ef860a24SChandler Carruth   return getSuccessor(idx);
3259*ef860a24SChandler Carruth }
3260*ef860a24SChandler Carruth unsigned SwitchInst::getNumSuccessorsV() const {
3261*ef860a24SChandler Carruth   return getNumSuccessors();
3262*ef860a24SChandler Carruth }
3263*ef860a24SChandler Carruth void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3264*ef860a24SChandler Carruth   setSuccessor(idx, B);
3265*ef860a24SChandler Carruth }
3266*ef860a24SChandler Carruth 
3267*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
3268*ef860a24SChandler Carruth //                        IndirectBrInst Implementation
3269*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
3270*ef860a24SChandler Carruth 
3271*ef860a24SChandler Carruth void IndirectBrInst::init(Value *Address, unsigned NumDests) {
3272*ef860a24SChandler Carruth   assert(Address && Address->getType()->isPointerTy() &&
3273*ef860a24SChandler Carruth          "Address of indirectbr must be a pointer");
3274*ef860a24SChandler Carruth   ReservedSpace = 1+NumDests;
3275*ef860a24SChandler Carruth   NumOperands = 1;
3276*ef860a24SChandler Carruth   OperandList = allocHungoffUses(ReservedSpace);
3277*ef860a24SChandler Carruth 
3278*ef860a24SChandler Carruth   OperandList[0] = Address;
3279*ef860a24SChandler Carruth }
3280*ef860a24SChandler Carruth 
3281*ef860a24SChandler Carruth 
3282*ef860a24SChandler Carruth /// growOperands - grow operands - This grows the operand list in response
3283*ef860a24SChandler Carruth /// to a push_back style of operation.  This grows the number of ops by 2 times.
3284*ef860a24SChandler Carruth ///
3285*ef860a24SChandler Carruth void IndirectBrInst::growOperands() {
3286*ef860a24SChandler Carruth   unsigned e = getNumOperands();
3287*ef860a24SChandler Carruth   unsigned NumOps = e*2;
3288*ef860a24SChandler Carruth 
3289*ef860a24SChandler Carruth   ReservedSpace = NumOps;
3290*ef860a24SChandler Carruth   Use *NewOps = allocHungoffUses(NumOps);
3291*ef860a24SChandler Carruth   Use *OldOps = OperandList;
3292*ef860a24SChandler Carruth   for (unsigned i = 0; i != e; ++i)
3293*ef860a24SChandler Carruth     NewOps[i] = OldOps[i];
3294*ef860a24SChandler Carruth   OperandList = NewOps;
3295*ef860a24SChandler Carruth   Use::zap(OldOps, OldOps + e, true);
3296*ef860a24SChandler Carruth }
3297*ef860a24SChandler Carruth 
3298*ef860a24SChandler Carruth IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3299*ef860a24SChandler Carruth                                Instruction *InsertBefore)
3300*ef860a24SChandler Carruth : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3301*ef860a24SChandler Carruth                  0, 0, InsertBefore) {
3302*ef860a24SChandler Carruth   init(Address, NumCases);
3303*ef860a24SChandler Carruth }
3304*ef860a24SChandler Carruth 
3305*ef860a24SChandler Carruth IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3306*ef860a24SChandler Carruth                                BasicBlock *InsertAtEnd)
3307*ef860a24SChandler Carruth : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
3308*ef860a24SChandler Carruth                  0, 0, InsertAtEnd) {
3309*ef860a24SChandler Carruth   init(Address, NumCases);
3310*ef860a24SChandler Carruth }
3311*ef860a24SChandler Carruth 
3312*ef860a24SChandler Carruth IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3313*ef860a24SChandler Carruth   : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3314*ef860a24SChandler Carruth                    allocHungoffUses(IBI.getNumOperands()),
3315*ef860a24SChandler Carruth                    IBI.getNumOperands()) {
3316*ef860a24SChandler Carruth   Use *OL = OperandList, *InOL = IBI.OperandList;
3317*ef860a24SChandler Carruth   for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3318*ef860a24SChandler Carruth     OL[i] = InOL[i];
3319*ef860a24SChandler Carruth   SubclassOptionalData = IBI.SubclassOptionalData;
3320*ef860a24SChandler Carruth }
3321*ef860a24SChandler Carruth 
3322*ef860a24SChandler Carruth IndirectBrInst::~IndirectBrInst() {
3323*ef860a24SChandler Carruth   dropHungoffUses();
3324*ef860a24SChandler Carruth }
3325*ef860a24SChandler Carruth 
3326*ef860a24SChandler Carruth /// addDestination - Add a destination.
3327*ef860a24SChandler Carruth ///
3328*ef860a24SChandler Carruth void IndirectBrInst::addDestination(BasicBlock *DestBB) {
3329*ef860a24SChandler Carruth   unsigned OpNo = NumOperands;
3330*ef860a24SChandler Carruth   if (OpNo+1 > ReservedSpace)
3331*ef860a24SChandler Carruth     growOperands();  // Get more space!
3332*ef860a24SChandler Carruth   // Initialize some new operands.
3333*ef860a24SChandler Carruth   assert(OpNo < ReservedSpace && "Growing didn't work!");
3334*ef860a24SChandler Carruth   NumOperands = OpNo+1;
3335*ef860a24SChandler Carruth   OperandList[OpNo] = DestBB;
3336*ef860a24SChandler Carruth }
3337*ef860a24SChandler Carruth 
3338*ef860a24SChandler Carruth /// removeDestination - This method removes the specified successor from the
3339*ef860a24SChandler Carruth /// indirectbr instruction.
3340*ef860a24SChandler Carruth void IndirectBrInst::removeDestination(unsigned idx) {
3341*ef860a24SChandler Carruth   assert(idx < getNumOperands()-1 && "Successor index out of range!");
3342*ef860a24SChandler Carruth 
3343*ef860a24SChandler Carruth   unsigned NumOps = getNumOperands();
3344*ef860a24SChandler Carruth   Use *OL = OperandList;
3345*ef860a24SChandler Carruth 
3346*ef860a24SChandler Carruth   // Replace this value with the last one.
3347*ef860a24SChandler Carruth   OL[idx+1] = OL[NumOps-1];
3348*ef860a24SChandler Carruth 
3349*ef860a24SChandler Carruth   // Nuke the last value.
3350*ef860a24SChandler Carruth   OL[NumOps-1].set(0);
3351*ef860a24SChandler Carruth   NumOperands = NumOps-1;
3352*ef860a24SChandler Carruth }
3353*ef860a24SChandler Carruth 
3354*ef860a24SChandler Carruth BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
3355*ef860a24SChandler Carruth   return getSuccessor(idx);
3356*ef860a24SChandler Carruth }
3357*ef860a24SChandler Carruth unsigned IndirectBrInst::getNumSuccessorsV() const {
3358*ef860a24SChandler Carruth   return getNumSuccessors();
3359*ef860a24SChandler Carruth }
3360*ef860a24SChandler Carruth void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3361*ef860a24SChandler Carruth   setSuccessor(idx, B);
3362*ef860a24SChandler Carruth }
3363*ef860a24SChandler Carruth 
3364*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
3365*ef860a24SChandler Carruth //                           clone_impl() implementations
3366*ef860a24SChandler Carruth //===----------------------------------------------------------------------===//
3367*ef860a24SChandler Carruth 
3368*ef860a24SChandler Carruth // Define these methods here so vtables don't get emitted into every translation
3369*ef860a24SChandler Carruth // unit that uses these classes.
3370*ef860a24SChandler Carruth 
3371*ef860a24SChandler Carruth GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3372*ef860a24SChandler Carruth   return new (getNumOperands()) GetElementPtrInst(*this);
3373*ef860a24SChandler Carruth }
3374*ef860a24SChandler Carruth 
3375*ef860a24SChandler Carruth BinaryOperator *BinaryOperator::clone_impl() const {
3376*ef860a24SChandler Carruth   return Create(getOpcode(), Op<0>(), Op<1>());
3377*ef860a24SChandler Carruth }
3378*ef860a24SChandler Carruth 
3379*ef860a24SChandler Carruth FCmpInst* FCmpInst::clone_impl() const {
3380*ef860a24SChandler Carruth   return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
3381*ef860a24SChandler Carruth }
3382*ef860a24SChandler Carruth 
3383*ef860a24SChandler Carruth ICmpInst* ICmpInst::clone_impl() const {
3384*ef860a24SChandler Carruth   return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
3385*ef860a24SChandler Carruth }
3386*ef860a24SChandler Carruth 
3387*ef860a24SChandler Carruth ExtractValueInst *ExtractValueInst::clone_impl() const {
3388*ef860a24SChandler Carruth   return new ExtractValueInst(*this);
3389*ef860a24SChandler Carruth }
3390*ef860a24SChandler Carruth 
3391*ef860a24SChandler Carruth InsertValueInst *InsertValueInst::clone_impl() const {
3392*ef860a24SChandler Carruth   return new InsertValueInst(*this);
3393*ef860a24SChandler Carruth }
3394*ef860a24SChandler Carruth 
3395*ef860a24SChandler Carruth AllocaInst *AllocaInst::clone_impl() const {
3396*ef860a24SChandler Carruth   return new AllocaInst(getAllocatedType(),
3397*ef860a24SChandler Carruth                         (Value*)getOperand(0),
3398*ef860a24SChandler Carruth                         getAlignment());
3399*ef860a24SChandler Carruth }
3400*ef860a24SChandler Carruth 
3401*ef860a24SChandler Carruth LoadInst *LoadInst::clone_impl() const {
3402*ef860a24SChandler Carruth   return new LoadInst(getOperand(0), Twine(), isVolatile(),
3403*ef860a24SChandler Carruth                       getAlignment(), getOrdering(), getSynchScope());
3404*ef860a24SChandler Carruth }
3405*ef860a24SChandler Carruth 
3406*ef860a24SChandler Carruth StoreInst *StoreInst::clone_impl() const {
3407*ef860a24SChandler Carruth   return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
3408*ef860a24SChandler Carruth                        getAlignment(), getOrdering(), getSynchScope());
3409*ef860a24SChandler Carruth 
3410*ef860a24SChandler Carruth }
3411*ef860a24SChandler Carruth 
3412*ef860a24SChandler Carruth AtomicCmpXchgInst *AtomicCmpXchgInst::clone_impl() const {
3413*ef860a24SChandler Carruth   AtomicCmpXchgInst *Result =
3414*ef860a24SChandler Carruth     new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
3415*ef860a24SChandler Carruth                           getOrdering(), getSynchScope());
3416*ef860a24SChandler Carruth   Result->setVolatile(isVolatile());
3417*ef860a24SChandler Carruth   return Result;
3418*ef860a24SChandler Carruth }
3419*ef860a24SChandler Carruth 
3420*ef860a24SChandler Carruth AtomicRMWInst *AtomicRMWInst::clone_impl() const {
3421*ef860a24SChandler Carruth   AtomicRMWInst *Result =
3422*ef860a24SChandler Carruth     new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3423*ef860a24SChandler Carruth                       getOrdering(), getSynchScope());
3424*ef860a24SChandler Carruth   Result->setVolatile(isVolatile());
3425*ef860a24SChandler Carruth   return Result;
3426*ef860a24SChandler Carruth }
3427*ef860a24SChandler Carruth 
3428*ef860a24SChandler Carruth FenceInst *FenceInst::clone_impl() const {
3429*ef860a24SChandler Carruth   return new FenceInst(getContext(), getOrdering(), getSynchScope());
3430*ef860a24SChandler Carruth }
3431*ef860a24SChandler Carruth 
3432*ef860a24SChandler Carruth TruncInst *TruncInst::clone_impl() const {
3433*ef860a24SChandler Carruth   return new TruncInst(getOperand(0), getType());
3434*ef860a24SChandler Carruth }
3435*ef860a24SChandler Carruth 
3436*ef860a24SChandler Carruth ZExtInst *ZExtInst::clone_impl() const {
3437*ef860a24SChandler Carruth   return new ZExtInst(getOperand(0), getType());
3438*ef860a24SChandler Carruth }
3439*ef860a24SChandler Carruth 
3440*ef860a24SChandler Carruth SExtInst *SExtInst::clone_impl() const {
3441*ef860a24SChandler Carruth   return new SExtInst(getOperand(0), getType());
3442*ef860a24SChandler Carruth }
3443*ef860a24SChandler Carruth 
3444*ef860a24SChandler Carruth FPTruncInst *FPTruncInst::clone_impl() const {
3445*ef860a24SChandler Carruth   return new FPTruncInst(getOperand(0), getType());
3446*ef860a24SChandler Carruth }
3447*ef860a24SChandler Carruth 
3448*ef860a24SChandler Carruth FPExtInst *FPExtInst::clone_impl() const {
3449*ef860a24SChandler Carruth   return new FPExtInst(getOperand(0), getType());
3450*ef860a24SChandler Carruth }
3451*ef860a24SChandler Carruth 
3452*ef860a24SChandler Carruth UIToFPInst *UIToFPInst::clone_impl() const {
3453*ef860a24SChandler Carruth   return new UIToFPInst(getOperand(0), getType());
3454*ef860a24SChandler Carruth }
3455*ef860a24SChandler Carruth 
3456*ef860a24SChandler Carruth SIToFPInst *SIToFPInst::clone_impl() const {
3457*ef860a24SChandler Carruth   return new SIToFPInst(getOperand(0), getType());
3458*ef860a24SChandler Carruth }
3459*ef860a24SChandler Carruth 
3460*ef860a24SChandler Carruth FPToUIInst *FPToUIInst::clone_impl() const {
3461*ef860a24SChandler Carruth   return new FPToUIInst(getOperand(0), getType());
3462*ef860a24SChandler Carruth }
3463*ef860a24SChandler Carruth 
3464*ef860a24SChandler Carruth FPToSIInst *FPToSIInst::clone_impl() const {
3465*ef860a24SChandler Carruth   return new FPToSIInst(getOperand(0), getType());
3466*ef860a24SChandler Carruth }
3467*ef860a24SChandler Carruth 
3468*ef860a24SChandler Carruth PtrToIntInst *PtrToIntInst::clone_impl() const {
3469*ef860a24SChandler Carruth   return new PtrToIntInst(getOperand(0), getType());
3470*ef860a24SChandler Carruth }
3471*ef860a24SChandler Carruth 
3472*ef860a24SChandler Carruth IntToPtrInst *IntToPtrInst::clone_impl() const {
3473*ef860a24SChandler Carruth   return new IntToPtrInst(getOperand(0), getType());
3474*ef860a24SChandler Carruth }
3475*ef860a24SChandler Carruth 
3476*ef860a24SChandler Carruth BitCastInst *BitCastInst::clone_impl() const {
3477*ef860a24SChandler Carruth   return new BitCastInst(getOperand(0), getType());
3478*ef860a24SChandler Carruth }
3479*ef860a24SChandler Carruth 
3480*ef860a24SChandler Carruth CallInst *CallInst::clone_impl() const {
3481*ef860a24SChandler Carruth   return  new(getNumOperands()) CallInst(*this);
3482*ef860a24SChandler Carruth }
3483*ef860a24SChandler Carruth 
3484*ef860a24SChandler Carruth SelectInst *SelectInst::clone_impl() const {
3485*ef860a24SChandler Carruth   return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
3486*ef860a24SChandler Carruth }
3487*ef860a24SChandler Carruth 
3488*ef860a24SChandler Carruth VAArgInst *VAArgInst::clone_impl() const {
3489*ef860a24SChandler Carruth   return new VAArgInst(getOperand(0), getType());
3490*ef860a24SChandler Carruth }
3491*ef860a24SChandler Carruth 
3492*ef860a24SChandler Carruth ExtractElementInst *ExtractElementInst::clone_impl() const {
3493*ef860a24SChandler Carruth   return ExtractElementInst::Create(getOperand(0), getOperand(1));
3494*ef860a24SChandler Carruth }
3495*ef860a24SChandler Carruth 
3496*ef860a24SChandler Carruth InsertElementInst *InsertElementInst::clone_impl() const {
3497*ef860a24SChandler Carruth   return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
3498*ef860a24SChandler Carruth }
3499*ef860a24SChandler Carruth 
3500*ef860a24SChandler Carruth ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3501*ef860a24SChandler Carruth   return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
3502*ef860a24SChandler Carruth }
3503*ef860a24SChandler Carruth 
3504*ef860a24SChandler Carruth PHINode *PHINode::clone_impl() const {
3505*ef860a24SChandler Carruth   return new PHINode(*this);
3506*ef860a24SChandler Carruth }
3507*ef860a24SChandler Carruth 
3508*ef860a24SChandler Carruth LandingPadInst *LandingPadInst::clone_impl() const {
3509*ef860a24SChandler Carruth   return new LandingPadInst(*this);
3510*ef860a24SChandler Carruth }
3511*ef860a24SChandler Carruth 
3512*ef860a24SChandler Carruth ReturnInst *ReturnInst::clone_impl() const {
3513*ef860a24SChandler Carruth   return new(getNumOperands()) ReturnInst(*this);
3514*ef860a24SChandler Carruth }
3515*ef860a24SChandler Carruth 
3516*ef860a24SChandler Carruth BranchInst *BranchInst::clone_impl() const {
3517*ef860a24SChandler Carruth   return new(getNumOperands()) BranchInst(*this);
3518*ef860a24SChandler Carruth }
3519*ef860a24SChandler Carruth 
3520*ef860a24SChandler Carruth SwitchInst *SwitchInst::clone_impl() const {
3521*ef860a24SChandler Carruth   return new SwitchInst(*this);
3522*ef860a24SChandler Carruth }
3523*ef860a24SChandler Carruth 
3524*ef860a24SChandler Carruth IndirectBrInst *IndirectBrInst::clone_impl() const {
3525*ef860a24SChandler Carruth   return new IndirectBrInst(*this);
3526*ef860a24SChandler Carruth }
3527*ef860a24SChandler Carruth 
3528*ef860a24SChandler Carruth 
3529*ef860a24SChandler Carruth InvokeInst *InvokeInst::clone_impl() const {
3530*ef860a24SChandler Carruth   return new(getNumOperands()) InvokeInst(*this);
3531*ef860a24SChandler Carruth }
3532*ef860a24SChandler Carruth 
3533*ef860a24SChandler Carruth ResumeInst *ResumeInst::clone_impl() const {
3534*ef860a24SChandler Carruth   return new(1) ResumeInst(*this);
3535*ef860a24SChandler Carruth }
3536*ef860a24SChandler Carruth 
3537*ef860a24SChandler Carruth UnreachableInst *UnreachableInst::clone_impl() const {
3538*ef860a24SChandler Carruth   LLVMContext &Context = getContext();
3539*ef860a24SChandler Carruth   return new UnreachableInst(Context);
3540*ef860a24SChandler Carruth }
3541