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