blob: f64b220c3fde3e733158d6abca458a22989158bd [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Devang Pateladd58652009-09-23 18:32:25 +000015#include "LLVMContextImpl.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000016#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Evan Cheng1d9d4bd2009-09-10 04:36:43 +000020#include "llvm/Module.h"
Dan Gohmand2a251f2009-07-17 21:33:58 +000021#include "llvm/Operator.h"
Dan Gohman22571482009-09-03 15:34:35 +000022#include "llvm/Analysis/Dominators.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000025#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Eric Christopher7258dcd2010-04-16 23:37:20 +000036 ? cast<CallInst>(II)->op_begin()
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Function
38}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
65
66 if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
67 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000068 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000069 return "vector select condition element type must be i1";
70 const VectorType *ET = dyn_cast<VectorType>(Op1->getType());
71 if (ET == 0)
72 return "selected values for vector select must be vectors";
73 if (ET->getNumElements() != VT->getNumElements())
74 return "vector select requires selected vectors to have "
75 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000076 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000077 return "select condition must be i1 or <n x i1>";
78 }
79 return 0;
80}
81
82
83//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000084// PHINode Class
85//===----------------------------------------------------------------------===//
86
87PHINode::PHINode(const PHINode &PN)
88 : Instruction(PN.getType(), Instruction::PHI,
Gabor Greiff6caff662008-05-10 08:32:32 +000089 allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()),
Chris Lattnerafdb3de2005-01-29 00:35:16 +000090 ReservedSpace(PN.getNumOperands()) {
91 Use *OL = OperandList;
92 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +000093 OL[i] = PN.getOperand(i);
94 OL[i+1] = PN.getOperand(i+1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +000095 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +000096 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000097}
98
Gordon Henriksen14a55692007-12-10 02:14:30 +000099PHINode::~PHINode() {
Chris Lattner7d6aac82008-06-16 04:02:40 +0000100 if (OperandList)
101 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000102}
103
104// removeIncomingValue - Remove an incoming value. This is useful if a
105// predecessor basic block is deleted.
106Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
107 unsigned NumOps = getNumOperands();
108 Use *OL = OperandList;
109 assert(Idx*2 < NumOps && "BB not in PHI node!");
110 Value *Removed = OL[Idx*2];
111
112 // Move everything after this operand down.
113 //
114 // FIXME: we could just swap with the end of the list, then erase. However,
115 // client might not expect this to happen. The code as it is thrashes the
116 // use/def lists, which is kinda lame.
117 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
118 OL[i-2] = OL[i];
119 OL[i-2+1] = OL[i+1];
120 }
121
122 // Nuke the last value.
123 OL[NumOps-2].set(0);
124 OL[NumOps-2+1].set(0);
125 NumOperands = NumOps-2;
126
127 // If the PHI node is dead, because it has zero entries, nuke it now.
128 if (NumOps == 2 && DeletePHIIfEmpty) {
129 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000130 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000131 eraseFromParent();
132 }
133 return Removed;
134}
135
136/// resizeOperands - resize operands - This adjusts the length of the operands
137/// list according to the following behavior:
138/// 1. If NumOps == 0, grow the operand list in response to a push_back style
139/// of operation. This grows the number of ops by 1.5 times.
140/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
141/// 3. If NumOps == NumOperands, trim the reserved space.
142///
143void PHINode::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000144 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000145 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000146 NumOps = e*3/2;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000147 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
148 } else if (NumOps*2 > NumOperands) {
149 // No resize needed.
150 if (ReservedSpace >= NumOps) return;
151 } else if (NumOps == NumOperands) {
152 if (ReservedSpace == NumOps) return;
153 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000155 }
156
157 ReservedSpace = NumOps;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000158 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +0000159 Use *NewOps = allocHungoffUses(NumOps);
Dan Gohman031f0bb2008-06-23 16:45:24 +0000160 std::copy(OldOps, OldOps + e, NewOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000161 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +0000162 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000163}
164
Nate Begemanb3923212005-08-04 23:24:19 +0000165/// hasConstantValue - If the specified PHI node always merges together the same
166/// value, return the value, otherwise return null.
167///
Dan Gohman22571482009-09-03 15:34:35 +0000168/// If the PHI has undef operands, but all the rest of the operands are
169/// some unique value, return that value if it can be proved that the
170/// value dominates the PHI. If DT is null, use a conservative check,
171/// otherwise use DT to test for dominance.
172///
173Value *PHINode::hasConstantValue(DominatorTree *DT) const {
Chris Lattner7d08da62009-09-21 22:27:34 +0000174 // If the PHI node only has one incoming value, eliminate the PHI node.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000175 if (getNumIncomingValues() == 1) {
Chris Lattner6e709c12005-08-05 15:37:31 +0000176 if (getIncomingValue(0) != this) // not X = phi X
177 return getIncomingValue(0);
Chris Lattner7d08da62009-09-21 22:27:34 +0000178 return UndefValue::get(getType()); // Self cycle is dead.
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000179 }
Chris Lattner6e709c12005-08-05 15:37:31 +0000180
Nate Begemanb3923212005-08-04 23:24:19 +0000181 // Otherwise if all of the incoming values are the same for the PHI, replace
182 // the PHI node with the incoming value.
183 //
184 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000185 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000186 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000187 if (isa<UndefValue>(getIncomingValue(i))) {
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000188 HasUndefInput = true;
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000189 } else if (getIncomingValue(i) != this) { // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000190 if (InVal && getIncomingValue(i) != InVal)
191 return 0; // Not the same, bail out.
Chris Lattner7d08da62009-09-21 22:27:34 +0000192 InVal = getIncomingValue(i);
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000193 }
Nate Begemanb3923212005-08-04 23:24:19 +0000194
195 // The only case that could cause InVal to be null is if we have a PHI node
196 // that only has entries for itself. In this case, there is no entry into the
197 // loop, so kill the PHI.
198 //
Owen Andersonb292b8c2009-07-30 23:03:37 +0000199 if (InVal == 0) InVal = UndefValue::get(getType());
Nate Begemanb3923212005-08-04 23:24:19 +0000200
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000201 // If we have a PHI node like phi(X, undef, X), where X is defined by some
202 // instruction, we cannot always return X as the result of the PHI node. Only
203 // do this if X is not an instruction (thus it must dominate the PHI block),
204 // or if the client is prepared to deal with this possibility.
Chris Lattner7d08da62009-09-21 22:27:34 +0000205 if (!HasUndefInput || !isa<Instruction>(InVal))
206 return InVal;
207
208 Instruction *IV = cast<Instruction>(InVal);
209 if (DT) {
210 // We have a DominatorTree. Do a precise test.
211 if (!DT->dominates(IV, this))
212 return 0;
213 } else {
214 // If it is in the entry block, it obviously dominates everything.
215 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
216 isa<InvokeInst>(IV))
217 return 0; // Cannot guarantee that InVal dominates this PHINode.
218 }
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000219
Nate Begemanb3923212005-08-04 23:24:19 +0000220 // All of the incoming values are the same, return the value now.
221 return InVal;
222}
223
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000224
225//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000226// CallInst Implementation
227//===----------------------------------------------------------------------===//
228
Gordon Henriksen14a55692007-12-10 02:14:30 +0000229CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000230}
231
Chris Lattner054ba2c2007-02-13 00:58:44 +0000232void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000233 assert(NumOperands == NumParams+1 && "NumOperands not set up?");
Eric Christopher7258dcd2010-04-16 23:37:20 +0000234 Use *OL = OperandList;
235 OL[0] = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236
Misha Brukmanb1c93172005-04-21 23:48:37 +0000237 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000238 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000239 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000240
Chris Lattner054ba2c2007-02-13 00:58:44 +0000241 assert((NumParams == FTy->getNumParams() ||
242 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000243 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000244 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000245 assert((i >= FTy->getNumParams() ||
246 FTy->getParamType(i) == Params[i]->getType()) &&
247 "Calling a function with a bad signature!");
Eric Christopher7258dcd2010-04-16 23:37:20 +0000248 OL[i+1] = Params[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000249 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000250}
251
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000252void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000253 assert(NumOperands == 3 && "NumOperands not set up?");
Eric Christopher7258dcd2010-04-16 23:37:20 +0000254 Use *OL = OperandList;
255 OL[0] = Func;
256 OL[1] = Actual1;
257 OL[2] = Actual2;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000258
259 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000260 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000261 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000263 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000264 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000265 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000266 assert((0 >= FTy->getNumParams() ||
267 FTy->getParamType(0) == Actual1->getType()) &&
268 "Calling a function with a bad signature!");
269 assert((1 >= FTy->getNumParams() ||
270 FTy->getParamType(1) == Actual2->getType()) &&
271 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272}
273
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000274void CallInst::init(Value *Func, Value *Actual) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000275 assert(NumOperands == 2 && "NumOperands not set up?");
Eric Christopher7258dcd2010-04-16 23:37:20 +0000276 Use *OL = OperandList;
277 OL[0] = Func;
278 OL[1] = Actual;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000279
280 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000282 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000283
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000284 assert((FTy->getNumParams() == 1 ||
285 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000287 assert((0 == FTy->getNumParams() ||
288 FTy->getParamType(0) == Actual->getType()) &&
289 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000290}
291
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000292void CallInst::init(Value *Func) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000293 assert(NumOperands == 1 && "NumOperands not set up?");
Eric Christopher7258dcd2010-04-16 23:37:20 +0000294 Use *OL = OperandList;
295 OL[0] = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000296
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000297 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000299 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000301 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000302}
303
Daniel Dunbar4975db62009-07-25 04:41:11 +0000304CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000305 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000306 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
307 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000308 Instruction::Call,
309 OperandTraits<CallInst>::op_end(this) - 2,
310 2, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000312 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313}
314
Daniel Dunbar4975db62009-07-25 04:41:11 +0000315CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000316 BasicBlock *InsertAtEnd)
317 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
318 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000319 Instruction::Call,
320 OperandTraits<CallInst>::op_end(this) - 2,
321 2, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000323 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000324}
Daniel Dunbar4975db62009-07-25 04:41:11 +0000325CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000326 Instruction *InsertBefore)
327 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
328 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000329 Instruction::Call,
330 OperandTraits<CallInst>::op_end(this) - 1,
331 1, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000332 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000333 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000334}
335
Daniel Dunbar4975db62009-07-25 04:41:11 +0000336CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337 BasicBlock *InsertAtEnd)
338 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
339 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000340 Instruction::Call,
341 OperandTraits<CallInst>::op_end(this) - 1,
342 1, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000343 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000344 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345}
346
Misha Brukmanb1c93172005-04-21 23:48:37 +0000347CallInst::CallInst(const CallInst &CI)
Gabor Greiff6caff662008-05-10 08:32:32 +0000348 : Instruction(CI.getType(), Instruction::Call,
349 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
Chris Lattner8a923e72008-03-12 17:45:29 +0000350 CI.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000351 setAttributes(CI.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000352 setTailCall(CI.isTailCall());
353 setCallingConv(CI.getCallingConv());
354
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000355 Use *OL = OperandList;
356 Use *InOL = CI.OperandList;
357 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000358 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000359 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000360}
361
Devang Patel4c758ea2008-09-25 21:00:45 +0000362void CallInst::addAttribute(unsigned i, Attributes attr) {
363 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000364 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000365 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000366}
367
Devang Patel4c758ea2008-09-25 21:00:45 +0000368void CallInst::removeAttribute(unsigned i, Attributes attr) {
369 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000370 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000371 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000372}
373
Devang Patelba3fa6c2008-09-23 23:03:40 +0000374bool CallInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000375 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000376 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000377 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000378 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000379 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000380}
381
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000382/// IsConstantOne - Return true only if val is constant int 1
383static bool IsConstantOne(Value *val) {
384 assert(val && "IsConstantOne does not work with NULL val");
385 return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne();
386}
387
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000388static Instruction *createMalloc(Instruction *InsertBefore,
389 BasicBlock *InsertAtEnd, const Type *IntPtrTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000390 const Type *AllocTy, Value *AllocSize,
391 Value *ArraySize, Function *MallocF,
392 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000393 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000394 "createMalloc needs either InsertBefore or InsertAtEnd");
395
396 // malloc(type) becomes:
397 // bitcast (i8* malloc(typeSize)) to type*
398 // malloc(type, arraySize) becomes:
399 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000400 if (!ArraySize)
401 ArraySize = ConstantInt::get(IntPtrTy, 1);
402 else if (ArraySize->getType() != IntPtrTy) {
403 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000404 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
405 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000406 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000407 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
408 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000409 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000410
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000411 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000412 if (IsConstantOne(AllocSize)) {
413 AllocSize = ArraySize; // Operand * 1 = Operand
414 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
415 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
416 false /*ZExt*/);
417 // Malloc arg is constant product of type size and array size
418 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
419 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000420 // Multiply type size by the array size...
421 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000422 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
423 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000424 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000425 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
426 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000428 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000429
Victor Hernandez788eaab2009-09-18 19:20:02 +0000430 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000431 // Create the call to Malloc.
432 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
433 Module* M = BB->getParent()->getParent();
Duncan Sands9ed7b162009-10-06 15:40:36 +0000434 const Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000435 Value *MallocFunc = MallocF;
436 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000437 // prototype malloc as "void *malloc(size_t)"
Victor Hernandezbb336a12009-11-10 19:53:28 +0000438 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000439 const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000440 CallInst *MCall = NULL;
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000441 Instruction *Result = NULL;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000442 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000443 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000444 Result = MCall;
445 if (Result->getType() != AllocPtrType)
446 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000447 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000448 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000449 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000450 Result = MCall;
451 if (Result->getType() != AllocPtrType) {
452 InsertAtEnd->getInstList().push_back(MCall);
453 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000454 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000455 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000456 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000457 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000458 if (Function *F = dyn_cast<Function>(MallocFunc)) {
459 MCall->setCallingConv(F->getCallingConv());
460 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
461 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000462 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000463
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000464 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000465}
466
467/// CreateMalloc - Generate the IR for a call to malloc:
468/// 1. Compute the malloc call's argument as the specified type's size,
469/// possibly multiplied by the array size if the array size is not
470/// constant 1.
471/// 2. Call malloc with that argument.
472/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000473Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
474 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000475 Value *AllocSize, Value *ArraySize,
476 const Twine &Name) {
477 return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000478 ArraySize, NULL, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000479}
480
481/// CreateMalloc - Generate the IR for a call to malloc:
482/// 1. Compute the malloc call's argument as the specified type's size,
483/// possibly multiplied by the array size if the array size is not
484/// constant 1.
485/// 2. Call malloc with that argument.
486/// 3. Bitcast the result of the malloc call to the specified type.
487/// Note: This function does not add the bitcast to the basic block, that is the
488/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000489Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
490 const Type *IntPtrTy, const Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000491 Value *AllocSize, Value *ArraySize,
492 Function *MallocF, const Twine &Name) {
493 return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000494 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000495}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000496
Victor Hernandeze2971492009-10-24 04:23:03 +0000497static Instruction* createFree(Value* Source, Instruction *InsertBefore,
498 BasicBlock *InsertAtEnd) {
499 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
500 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000501 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000502 "Can not free something of nonpointer type!");
503
504 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
505 Module* M = BB->getParent()->getParent();
506
507 const Type *VoidTy = Type::getVoidTy(M->getContext());
508 const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
509 // prototype free as "void free(void*)"
Chris Lattner2156c222009-11-09 07:12:01 +0000510 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL);
Victor Hernandeze2971492009-10-24 04:23:03 +0000511 CallInst* Result = NULL;
512 Value *PtrCast = Source;
513 if (InsertBefore) {
514 if (Source->getType() != IntPtrTy)
515 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
516 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
517 } else {
518 if (Source->getType() != IntPtrTy)
519 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
520 Result = CallInst::Create(FreeFunc, PtrCast, "");
521 }
522 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000523 if (Function *F = dyn_cast<Function>(FreeFunc))
524 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000525
526 return Result;
527}
528
529/// CreateFree - Generate the IR for a call to the builtin free function.
530void CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
531 createFree(Source, InsertBefore, NULL);
532}
533
534/// CreateFree - Generate the IR for a call to the builtin free function.
535/// Note: This function does not add the call to the basic block, that is the
536/// responsibility of the caller.
537Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
538 Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd);
539 assert(FreeCall && "CreateFree did not create a CallInst");
540 return FreeCall;
541}
542
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000543//===----------------------------------------------------------------------===//
544// InvokeInst Implementation
545//===----------------------------------------------------------------------===//
546
547void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000548 Value* const *Args, unsigned NumArgs) {
Gabor Greiff6caff662008-05-10 08:32:32 +0000549 assert(NumOperands == 3+NumArgs && "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000550 Op<-3>() = Fn;
551 Op<-2>() = IfNormal;
552 Op<-1>() = IfException;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000553 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000554 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000555 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000556
Anton Korobeynikov579f0712008-02-20 11:08:44 +0000557 assert(((NumArgs == FTy->getNumParams()) ||
558 (FTy->isVarArg() && NumArgs > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000559 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000560
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000561 Use *OL = OperandList;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000562 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000563 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000564 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000565 "Invoking a function with a bad signature!");
566
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000567 OL[i] = Args[i];
Chris Lattner667a0562006-05-03 00:48:22 +0000568 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000569}
570
Misha Brukmanb1c93172005-04-21 23:48:37 +0000571InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000572 : TerminatorInst(II.getType(), Instruction::Invoke,
Gabor Greif697e94c2008-05-15 10:04:30 +0000573 OperandTraits<InvokeInst>::op_end(this)
574 - II.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000575 II.getNumOperands()) {
Devang Patel4c758ea2008-09-25 21:00:45 +0000576 setAttributes(II.getAttributes());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000577 setCallingConv(II.getCallingConv());
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000578 Use *OL = OperandList, *InOL = II.OperandList;
579 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +0000580 OL[i] = InOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000581 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000582}
583
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000584BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
585 return getSuccessor(idx);
586}
587unsigned InvokeInst::getNumSuccessorsV() const {
588 return getNumSuccessors();
589}
590void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
591 return setSuccessor(idx, B);
592}
593
Devang Patelba3fa6c2008-09-23 23:03:40 +0000594bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const {
Devang Patel4c758ea2008-09-25 21:00:45 +0000595 if (AttributeList.paramHasAttr(i, attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000596 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000597 if (const Function *F = getCalledFunction())
Dale Johannesen89268bc2008-02-19 21:38:47 +0000598 return F->paramHasAttr(i, attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000599 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000600}
601
Devang Patel4c758ea2008-09-25 21:00:45 +0000602void InvokeInst::addAttribute(unsigned i, Attributes attr) {
603 AttrListPtr PAL = getAttributes();
Eric Christopher901b1a72008-05-16 20:39:43 +0000604 PAL = PAL.addAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000605 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000606}
607
Devang Patel4c758ea2008-09-25 21:00:45 +0000608void InvokeInst::removeAttribute(unsigned i, Attributes attr) {
609 AttrListPtr PAL = getAttributes();
Duncan Sands78c88722008-07-08 08:38:44 +0000610 PAL = PAL.removeAttr(i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000611 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000612}
613
Duncan Sands5208d1a2007-11-28 17:07:01 +0000614
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000615//===----------------------------------------------------------------------===//
616// ReturnInst Implementation
617//===----------------------------------------------------------------------===//
618
Chris Lattner2195fc42007-02-24 00:55:48 +0000619ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000620 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000621 OperandTraits<ReturnInst>::op_end(this) -
622 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000623 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000624 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000625 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000626 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000627}
628
Owen Anderson55f1c092009-08-13 21:58:54 +0000629ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
630 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000631 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
632 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000633 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000634 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000635}
Owen Anderson55f1c092009-08-13 21:58:54 +0000636ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
637 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000638 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
639 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000640 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000641 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000642}
Owen Anderson55f1c092009-08-13 21:58:54 +0000643ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
644 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000645 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000646}
647
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000648unsigned ReturnInst::getNumSuccessorsV() const {
649 return getNumSuccessors();
650}
651
Devang Patelae682fb2008-02-26 17:56:20 +0000652/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
653/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000654void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000655 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000656}
657
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000659 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000660 return 0;
661}
662
Devang Patel59643e52008-02-23 00:35:18 +0000663ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000664}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000665
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000666//===----------------------------------------------------------------------===//
667// UnwindInst Implementation
668//===----------------------------------------------------------------------===//
669
Owen Anderson55f1c092009-08-13 21:58:54 +0000670UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore)
671 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
672 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000673}
Owen Anderson55f1c092009-08-13 21:58:54 +0000674UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
675 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind,
676 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000677}
678
679
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000680unsigned UnwindInst::getNumSuccessorsV() const {
681 return getNumSuccessors();
682}
683
684void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000685 llvm_unreachable("UnwindInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000686}
687
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000688BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000689 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000690 return 0;
691}
692
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000693//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000694// UnreachableInst Implementation
695//===----------------------------------------------------------------------===//
696
Owen Anderson55f1c092009-08-13 21:58:54 +0000697UnreachableInst::UnreachableInst(LLVMContext &Context,
698 Instruction *InsertBefore)
699 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
700 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000701}
Owen Anderson55f1c092009-08-13 21:58:54 +0000702UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
703 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
704 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000705}
706
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000707unsigned UnreachableInst::getNumSuccessorsV() const {
708 return getNumSuccessors();
709}
710
711void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000712 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000713}
714
715BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000716 llvm_unreachable("UnwindInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000718}
719
720//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721// BranchInst Implementation
722//===----------------------------------------------------------------------===//
723
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000724void BranchInst::AssertOK() {
725 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +0000726 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000728}
729
Chris Lattner2195fc42007-02-24 00:55:48 +0000730BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000731 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000732 OperandTraits<BranchInst>::op_end(this) - 1,
733 1, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000734 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000735 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000736}
737BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
738 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +0000739 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000740 OperandTraits<BranchInst>::op_end(this) - 3,
741 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000742 Op<-1>() = IfTrue;
743 Op<-2>() = IfFalse;
744 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000745#ifndef NDEBUG
746 AssertOK();
747#endif
748}
749
750BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000751 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000752 OperandTraits<BranchInst>::op_end(this) - 1,
753 1, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000754 assert(IfTrue != 0 && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000755 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +0000756}
757
758BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
759 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +0000760 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000761 OperandTraits<BranchInst>::op_end(this) - 3,
762 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000763 Op<-1>() = IfTrue;
764 Op<-2>() = IfFalse;
765 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +0000766#ifndef NDEBUG
767 AssertOK();
768#endif
769}
770
771
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000772BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +0000773 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +0000774 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
775 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000776 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777 if (BI.getNumOperands() != 1) {
778 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000779 Op<-3>() = BI.Op<-3>();
780 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000781 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000782 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000783}
784
Gabor Greifc91aa9b2009-03-12 18:34:49 +0000785
786Use* Use::getPrefix() {
787 PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev);
788 if (PotentialPrefix.getOpaqueValue())
789 return 0;
790
791 return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1);
792}
793
794BranchInst::~BranchInst() {
795 if (NumOperands == 1) {
796 if (Use *Prefix = OperandList->getPrefix()) {
797 Op<-1>() = 0;
798 //
799 // mark OperandList to have a special value for scrutiny
800 // by baseclass destructors and operator delete
801 OperandList = Prefix;
802 } else {
803 NumOperands = 3;
804 OperandList = op_begin();
805 }
806 }
807}
808
809
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000810BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
811 return getSuccessor(idx);
812}
813unsigned BranchInst::getNumSuccessorsV() const {
814 return getNumSuccessors();
815}
816void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
817 setSuccessor(idx, B);
818}
819
820
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000821//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +0000822// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000823//===----------------------------------------------------------------------===//
824
Owen Andersonb6b25302009-07-14 23:09:55 +0000825static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000826 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +0000827 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000828 else {
829 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000830 "Passed basic block into allocation size parameter! Use other ctor");
Duncan Sands9dff9be2010-02-15 16:12:20 +0000831 assert(Amt->getType()->isIntegerTy(32) &&
Victor Hernandeza3aaf852009-10-17 01:18:07 +0000832 "Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000833 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000834 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835}
836
Victor Hernandez8acf2952009-10-23 21:09:37 +0000837AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
838 const Twine &Name, Instruction *InsertBefore)
839 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
840 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
841 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000842 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000843 setName(Name);
844}
845
846AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize,
847 const Twine &Name, BasicBlock *InsertAtEnd)
848 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
849 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
850 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000851 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000852 setName(Name);
853}
854
855AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
856 Instruction *InsertBefore)
857 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
858 getAISize(Ty->getContext(), 0), InsertBefore) {
859 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000860 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000861 setName(Name);
862}
863
864AllocaInst::AllocaInst(const Type *Ty, const Twine &Name,
865 BasicBlock *InsertAtEnd)
866 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
867 getAISize(Ty->getContext(), 0), InsertAtEnd) {
868 setAlignment(0);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000869 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Victor Hernandez8acf2952009-10-23 21:09:37 +0000870 setName(Name);
871}
872
873AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
874 const Twine &Name, Instruction *InsertBefore)
875 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000876 getAISize(Ty->getContext(), ArraySize), InsertBefore) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000877 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000878 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000879 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000880}
881
Victor Hernandez8acf2952009-10-23 21:09:37 +0000882AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
883 const Twine &Name, BasicBlock *InsertAtEnd)
884 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
Owen Anderson4fdeba92009-07-15 23:53:25 +0000885 getAISize(Ty->getContext(), ArraySize), InsertAtEnd) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000886 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000887 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000888 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889}
890
Gordon Henriksen14a55692007-12-10 02:14:30 +0000891// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +0000892AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +0000893}
894
Victor Hernandez8acf2952009-10-23 21:09:37 +0000895void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +0000896 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +0000897 setInstructionSubclassData(Log2_32(Align) + 1);
Dan Gohmanaa583d72008-03-24 16:55:58 +0000898 assert(getAlignment() == Align && "Alignment representation error!");
899}
900
Victor Hernandez8acf2952009-10-23 21:09:37 +0000901bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000902 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
903 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000904 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000905}
906
Victor Hernandez8acf2952009-10-23 21:09:37 +0000907const Type *AllocaInst::getAllocatedType() const {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000908 return getType()->getElementType();
909}
910
Chris Lattner8b291e62008-11-26 02:54:17 +0000911/// isStaticAlloca - Return true if this alloca is in the entry block of the
912/// function and is a constant size. If so, the code generator will fold it
913/// into the prolog/epilog code, so it is basically free.
914bool AllocaInst::isStaticAlloca() const {
915 // Must be constant size.
916 if (!isa<ConstantInt>(getArraySize())) return false;
917
918 // Must be in the entry block.
919 const BasicBlock *Parent = getParent();
920 return Parent == &Parent->getParent()->front();
921}
922
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000923//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000924// LoadInst Implementation
925//===----------------------------------------------------------------------===//
926
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000927void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +0000928 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000929 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000930}
931
Daniel Dunbar4975db62009-07-25 04:41:11 +0000932LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000933 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000934 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000935 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000936 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000937 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000938 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000939}
940
Daniel Dunbar4975db62009-07-25 04:41:11 +0000941LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000942 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000943 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000944 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000945 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000946 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000947 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000948}
949
Daniel Dunbar4975db62009-07-25 04:41:11 +0000950LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000951 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000952 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000953 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000954 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000955 setAlignment(0);
956 AssertOK();
957 setName(Name);
958}
959
Daniel Dunbar4975db62009-07-25 04:41:11 +0000960LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +0000961 unsigned Align, Instruction *InsertBef)
962 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
963 Load, Ptr, InsertBef) {
964 setVolatile(isVolatile);
965 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000966 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000967 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000968}
969
Daniel Dunbar4975db62009-07-25 04:41:11 +0000970LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000971 unsigned Align, BasicBlock *InsertAE)
972 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
973 Load, Ptr, InsertAE) {
974 setVolatile(isVolatile);
975 setAlignment(Align);
976 AssertOK();
977 setName(Name);
978}
979
Daniel Dunbar4975db62009-07-25 04:41:11 +0000980LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000981 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000982 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000983 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000984 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000985 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000986 AssertOK();
987 setName(Name);
988}
989
Daniel Dunbar27096822009-08-11 18:11:15 +0000990
991
992LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
993 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
994 Load, Ptr, InsertBef) {
995 setVolatile(false);
996 setAlignment(0);
997 AssertOK();
998 if (Name && Name[0]) setName(Name);
999}
1000
1001LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1002 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1003 Load, Ptr, InsertAE) {
1004 setVolatile(false);
1005 setAlignment(0);
1006 AssertOK();
1007 if (Name && Name[0]) setName(Name);
1008}
1009
1010LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1011 Instruction *InsertBef)
1012: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1013 Load, Ptr, InsertBef) {
1014 setVolatile(isVolatile);
1015 setAlignment(0);
1016 AssertOK();
1017 if (Name && Name[0]) setName(Name);
1018}
1019
1020LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1021 BasicBlock *InsertAE)
1022 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1023 Load, Ptr, InsertAE) {
1024 setVolatile(isVolatile);
1025 setAlignment(0);
1026 AssertOK();
1027 if (Name && Name[0]) setName(Name);
1028}
1029
Christopher Lamb84485702007-04-22 19:24:39 +00001030void LoadInst::setAlignment(unsigned Align) {
1031 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001032 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1033 ((Log2_32(Align)+1)<<1));
Christopher Lamb84485702007-04-22 19:24:39 +00001034}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001035
1036//===----------------------------------------------------------------------===//
1037// StoreInst Implementation
1038//===----------------------------------------------------------------------===//
1039
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001040void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001041 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001042 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001043 "Ptr must have pointer type!");
1044 assert(getOperand(0)->getType() ==
1045 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001046 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001047}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001048
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001049
1050StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001051 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001052 OperandTraits<StoreInst>::op_begin(this),
1053 OperandTraits<StoreInst>::operands(this),
1054 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001055 Op<0>() = val;
1056 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001057 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001058 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001059 AssertOK();
1060}
1061
1062StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001063 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001064 OperandTraits<StoreInst>::op_begin(this),
1065 OperandTraits<StoreInst>::operands(this),
1066 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001067 Op<0>() = val;
1068 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001069 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +00001070 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001071 AssertOK();
1072}
1073
Misha Brukmanb1c93172005-04-21 23:48:37 +00001074StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001075 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001076 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001077 OperandTraits<StoreInst>::op_begin(this),
1078 OperandTraits<StoreInst>::operands(this),
1079 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001080 Op<0>() = val;
1081 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001082 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001083 setAlignment(0);
1084 AssertOK();
1085}
1086
1087StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
1088 unsigned Align, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001089 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001090 OperandTraits<StoreInst>::op_begin(this),
1091 OperandTraits<StoreInst>::operands(this),
1092 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001093 Op<0>() = val;
1094 Op<1>() = addr;
Christopher Lamb84485702007-04-22 19:24:39 +00001095 setVolatile(isVolatile);
1096 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001097 AssertOK();
1098}
1099
Misha Brukmanb1c93172005-04-21 23:48:37 +00001100StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001101 unsigned Align, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001102 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001103 OperandTraits<StoreInst>::op_begin(this),
1104 OperandTraits<StoreInst>::operands(this),
1105 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001106 Op<0>() = val;
1107 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001108 setVolatile(isVolatile);
1109 setAlignment(Align);
1110 AssertOK();
1111}
1112
1113StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001114 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001115 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001116 OperandTraits<StoreInst>::op_begin(this),
1117 OperandTraits<StoreInst>::operands(this),
1118 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001119 Op<0>() = val;
1120 Op<1>() = addr;
Chris Lattnerdf57a022005-02-05 01:38:38 +00001121 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +00001122 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001123 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001124}
1125
Christopher Lamb84485702007-04-22 19:24:39 +00001126void StoreInst::setAlignment(unsigned Align) {
1127 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001128 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1129 ((Log2_32(Align)+1) << 1));
Christopher Lamb84485702007-04-22 19:24:39 +00001130}
1131
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001132//===----------------------------------------------------------------------===//
1133// GetElementPtrInst Implementation
1134//===----------------------------------------------------------------------===//
1135
Christopher Lambedf07882007-12-17 01:12:55 +00001136static unsigned retrieveAddrSpace(const Value *Val) {
1137 return cast<PointerType>(Val->getType())->getAddressSpace();
1138}
1139
Gabor Greif21ba1842008-06-06 20:28:12 +00001140void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001141 const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001142 assert(NumOperands == 1+NumIdx && "NumOperands not initialized?");
1143 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001144 OL[0] = Ptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001145
Chris Lattner79807c3d2007-01-31 19:47:18 +00001146 for (unsigned i = 0; i != NumIdx; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001147 OL[i+1] = Idx[i];
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001148
1149 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001150}
1151
Daniel Dunbar4975db62009-07-25 04:41:11 +00001152void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) {
Gabor Greiff6caff662008-05-10 08:32:32 +00001153 assert(NumOperands == 2 && "NumOperands not initialized?");
1154 Use *OL = OperandList;
Gabor Greif2d3024d2008-05-26 21:33:52 +00001155 OL[0] = Ptr;
1156 OL[1] = Idx;
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001157
1158 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001159}
1160
Gabor Greiff6caff662008-05-10 08:32:32 +00001161GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
Gabor Greife9408e62008-05-27 11:03:29 +00001162 : Instruction(GEPI.getType(), GetElementPtr,
Gabor Greif697e94c2008-05-15 10:04:30 +00001163 OperandTraits<GetElementPtrInst>::op_end(this)
1164 - GEPI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001165 GEPI.getNumOperands()) {
1166 Use *OL = OperandList;
1167 Use *GEPIOL = GEPI.OperandList;
1168 for (unsigned i = 0, E = NumOperands; i != E; ++i)
Gabor Greif2d3024d2008-05-26 21:33:52 +00001169 OL[i] = GEPIOL[i];
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001170 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001171}
1172
Chris Lattner82981202005-05-03 05:43:30 +00001173GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001174 const Twine &Name, Instruction *InBe)
Owen Anderson4056ca92009-07-29 22:17:13 +00001175 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001176 checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001177 GetElementPtr,
1178 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1179 2, InBe) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001180 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001181}
1182
1183GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001184 const Twine &Name, BasicBlock *IAE)
Owen Anderson4056ca92009-07-29 22:17:13 +00001185 : Instruction(PointerType::get(
Owen Andersond420fd42009-07-16 00:03:07 +00001186 checkType(getIndexedType(Ptr->getType(),Idx)),
1187 retrieveAddrSpace(Ptr)),
Gabor Greiff6caff662008-05-10 08:32:32 +00001188 GetElementPtr,
1189 OperandTraits<GetElementPtrInst>::op_end(this) - 2,
1190 2, IAE) {
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001191 init(Ptr, Idx, Name);
Chris Lattner82981202005-05-03 05:43:30 +00001192}
1193
Chris Lattner6090a422009-03-09 04:46:40 +00001194/// getIndexedType - Returns the type of the element that would be accessed with
1195/// a gep instruction with the specified parameters.
1196///
1197/// The Idxs pointer should point to a continuous piece of memory containing the
1198/// indices, either as Value* or uint64_t.
1199///
1200/// A null type is returned if the indices are invalid for the specified
1201/// pointer type.
1202///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001203template <typename IndexTy>
Chris Lattner6090a422009-03-09 04:46:40 +00001204static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs,
1205 unsigned NumIdx) {
Dan Gohman12fce772008-05-15 19:50:34 +00001206 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1207 if (!PTy) return 0; // Type isn't a pointer type!
1208 const Type *Agg = PTy->getElementType();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001209
Chris Lattner6090a422009-03-09 04:46:40 +00001210 // Handle the special case of the empty set index set, which is always valid.
Dan Gohman12fce772008-05-15 19:50:34 +00001211 if (NumIdx == 0)
1212 return Agg;
Chris Lattner6090a422009-03-09 04:46:40 +00001213
1214 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattner4a488152009-03-09 04:56:22 +00001215 // it cannot be 'stepped over'. We explicitly allow abstract types (those
1216 // that contain opaque types) under the assumption that it will be resolved to
1217 // a sane type later.
1218 if (!Agg->isSized() && !Agg->isAbstract())
Chris Lattner6090a422009-03-09 04:46:40 +00001219 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001220
Dan Gohman1ecaf452008-05-31 00:58:22 +00001221 unsigned CurIdx = 1;
1222 for (; CurIdx != NumIdx; ++CurIdx) {
1223 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001224 if (!CT || CT->isPointerTy()) return 0;
Matthijs Kooijman04468622008-07-29 08:46:11 +00001225 IndexTy Index = Idxs[CurIdx];
Dan Gohman1ecaf452008-05-31 00:58:22 +00001226 if (!CT->indexValid(Index)) return 0;
1227 Agg = CT->getTypeAtIndex(Index);
1228
1229 // If the new type forwards to another type, then it is in the middle
1230 // of being refined to another type (and hence, may have dropped all
1231 // references to what it was using before). So, use the new forwarded
1232 // type.
1233 if (const Type *Ty = Agg->getForwardedType())
1234 Agg = Ty;
1235 }
1236 return CurIdx == NumIdx ? Agg : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001237}
1238
Matthijs Kooijman04468622008-07-29 08:46:11 +00001239const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1240 Value* const *Idxs,
1241 unsigned NumIdx) {
1242 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1243}
1244
1245const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
1246 uint64_t const *Idxs,
1247 unsigned NumIdx) {
1248 return getIndexedTypeInternal(Ptr, Idxs, NumIdx);
1249}
1250
Chris Lattner82981202005-05-03 05:43:30 +00001251const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1252 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1253 if (!PTy) return 0; // Type isn't a pointer type!
1254
1255 // Check the pointer index.
1256 if (!PTy->indexValid(Idx)) return 0;
1257
Chris Lattnerc2233332005-05-03 16:44:45 +00001258 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001259}
1260
Chris Lattner45f15572007-04-14 00:12:57 +00001261
1262/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1263/// zeros. If so, the result pointer and the first operand have the same
1264/// value, just potentially different types.
1265bool GetElementPtrInst::hasAllZeroIndices() const {
1266 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1267 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1268 if (!CI->isZero()) return false;
1269 } else {
1270 return false;
1271 }
1272 }
1273 return true;
1274}
1275
Chris Lattner27058292007-04-27 20:35:56 +00001276/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1277/// constant integers. If so, the result pointer and the first operand have
1278/// a constant offset between them.
1279bool GetElementPtrInst::hasAllConstantIndices() const {
1280 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1281 if (!isa<ConstantInt>(getOperand(i)))
1282 return false;
1283 }
1284 return true;
1285}
1286
Dan Gohman1b849082009-09-07 23:54:19 +00001287void GetElementPtrInst::setIsInBounds(bool B) {
1288 cast<GEPOperator>(this)->setIsInBounds(B);
1289}
Chris Lattner45f15572007-04-14 00:12:57 +00001290
Nick Lewycky28a5f252009-09-27 21:33:04 +00001291bool GetElementPtrInst::isInBounds() const {
1292 return cast<GEPOperator>(this)->isInBounds();
1293}
1294
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001295//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001296// ExtractElementInst Implementation
1297//===----------------------------------------------------------------------===//
1298
1299ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001300 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001301 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001302 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001303 ExtractElement,
1304 OperandTraits<ExtractElementInst>::op_begin(this),
1305 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001306 assert(isValidOperands(Val, Index) &&
1307 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001308 Op<0>() = Val;
1309 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001310 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001311}
1312
1313ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001314 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001315 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001316 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001317 ExtractElement,
1318 OperandTraits<ExtractElementInst>::op_begin(this),
1319 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001320 assert(isValidOperands(Val, Index) &&
1321 "Invalid extractelement instruction operands!");
1322
Gabor Greif2d3024d2008-05-26 21:33:52 +00001323 Op<0>() = Val;
1324 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001325 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001326}
1327
Chris Lattner65511ff2006-10-05 06:24:58 +00001328
Chris Lattner54865b32006-04-08 04:05:48 +00001329bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001330 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32))
Chris Lattner54865b32006-04-08 04:05:48 +00001331 return false;
1332 return true;
1333}
1334
1335
Robert Bocchino23004482006-01-10 19:05:34 +00001336//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001337// InsertElementInst Implementation
1338//===----------------------------------------------------------------------===//
1339
Chris Lattner54865b32006-04-08 04:05:48 +00001340InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001341 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001342 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001343 : Instruction(Vec->getType(), InsertElement,
1344 OperandTraits<InsertElementInst>::op_begin(this),
1345 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001346 assert(isValidOperands(Vec, Elt, Index) &&
1347 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001348 Op<0>() = Vec;
1349 Op<1>() = Elt;
1350 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001351 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001352}
1353
Chris Lattner54865b32006-04-08 04:05:48 +00001354InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001355 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001356 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001357 : Instruction(Vec->getType(), InsertElement,
1358 OperandTraits<InsertElementInst>::op_begin(this),
1359 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001360 assert(isValidOperands(Vec, Elt, Index) &&
1361 "Invalid insertelement instruction operands!");
1362
Gabor Greif2d3024d2008-05-26 21:33:52 +00001363 Op<0>() = Vec;
1364 Op<1>() = Elt;
1365 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001366 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001367}
1368
Chris Lattner54865b32006-04-08 04:05:48 +00001369bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1370 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001371 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001372 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001373
Reid Spencerd84d35b2007-02-15 02:26:10 +00001374 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001375 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001376
Duncan Sands9dff9be2010-02-15 16:12:20 +00001377 if (!Index->getType()->isIntegerTy(32))
Dan Gohman4fe64de2009-06-14 23:30:43 +00001378 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001379 return true;
1380}
1381
1382
Robert Bocchinoca27f032006-01-17 20:07:22 +00001383//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001384// ShuffleVectorInst Implementation
1385//===----------------------------------------------------------------------===//
1386
1387ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001388 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001389 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001390: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001391 cast<VectorType>(Mask->getType())->getNumElements()),
1392 ShuffleVector,
1393 OperandTraits<ShuffleVectorInst>::op_begin(this),
1394 OperandTraits<ShuffleVectorInst>::operands(this),
1395 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001396 assert(isValidOperands(V1, V2, Mask) &&
1397 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001398 Op<0>() = V1;
1399 Op<1>() = V2;
1400 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001401 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001402}
1403
1404ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001405 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001406 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001407: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1408 cast<VectorType>(Mask->getType())->getNumElements()),
1409 ShuffleVector,
1410 OperandTraits<ShuffleVectorInst>::op_begin(this),
1411 OperandTraits<ShuffleVectorInst>::operands(this),
1412 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001413 assert(isValidOperands(V1, V2, Mask) &&
1414 "Invalid shuffle vector instruction operands!");
1415
Gabor Greif2d3024d2008-05-26 21:33:52 +00001416 Op<0>() = V1;
1417 Op<1>() = V2;
1418 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001419 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001420}
1421
Mon P Wang25f01062008-11-10 04:46:22 +00001422bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001423 const Value *Mask) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001424 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001425 return false;
1426
1427 const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
1428 if (!isa<Constant>(Mask) || MaskTy == 0 ||
Duncan Sands9dff9be2010-02-15 16:12:20 +00001429 !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001430 return false;
1431 return true;
1432}
1433
Chris Lattnerf724e342008-03-02 05:28:33 +00001434/// getMaskValue - Return the index from the shuffle mask for the specified
1435/// output result. This is either -1 if the element is undef or a number less
1436/// than 2*numelements.
1437int ShuffleVectorInst::getMaskValue(unsigned i) const {
1438 const Constant *Mask = cast<Constant>(getOperand(2));
1439 if (isa<UndefValue>(Mask)) return -1;
1440 if (isa<ConstantAggregateZero>(Mask)) return 0;
1441 const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
1442 assert(i < MaskCV->getNumOperands() && "Index out of range");
1443
1444 if (isa<UndefValue>(MaskCV->getOperand(i)))
1445 return -1;
1446 return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
1447}
1448
Dan Gohman12fce772008-05-15 19:50:34 +00001449//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001450// InsertValueInst Class
1451//===----------------------------------------------------------------------===//
1452
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001453void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001454 unsigned NumIdx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001455 assert(NumOperands == 2 && "NumOperands not initialized?");
1456 Op<0>() = Agg;
1457 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001458
Dan Gohman1ecaf452008-05-31 00:58:22 +00001459 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001460 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001461}
1462
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001463void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001464 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001465 assert(NumOperands == 2 && "NumOperands not initialized?");
1466 Op<0>() = Agg;
1467 Op<1>() = Val;
1468
1469 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001470 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001471}
1472
1473InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001474 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001475 OperandTraits<InsertValueInst>::op_begin(this), 2),
1476 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001477 Op<0>() = IVI.getOperand(0);
1478 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001479 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001480}
1481
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001482InsertValueInst::InsertValueInst(Value *Agg,
1483 Value *Val,
1484 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001485 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001486 Instruction *InsertBefore)
1487 : Instruction(Agg->getType(), InsertValue,
1488 OperandTraits<InsertValueInst>::op_begin(this),
1489 2, InsertBefore) {
1490 init(Agg, Val, Idx, Name);
1491}
1492
1493InsertValueInst::InsertValueInst(Value *Agg,
1494 Value *Val,
1495 unsigned Idx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001496 const Twine &Name,
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001497 BasicBlock *InsertAtEnd)
1498 : Instruction(Agg->getType(), InsertValue,
1499 OperandTraits<InsertValueInst>::op_begin(this),
1500 2, InsertAtEnd) {
1501 init(Agg, Val, Idx, Name);
1502}
1503
Dan Gohman0752bff2008-05-23 00:36:11 +00001504//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001505// ExtractValueInst Class
1506//===----------------------------------------------------------------------===//
1507
Gabor Greifcbcc4952008-06-06 21:06:32 +00001508void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001509 const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001510 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001511
Dan Gohman1ecaf452008-05-31 00:58:22 +00001512 Indices.insert(Indices.end(), Idx, Idx + NumIdx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001513 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001514}
1515
Daniel Dunbar4975db62009-07-25 04:41:11 +00001516void ExtractValueInst::init(unsigned Idx, const Twine &Name) {
Dan Gohman1ecaf452008-05-31 00:58:22 +00001517 assert(NumOperands == 1 && "NumOperands not initialized?");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001518
1519 Indices.push_back(Idx);
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001520 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001521}
1522
1523ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001524 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001525 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001526 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001527}
1528
Dan Gohman12fce772008-05-15 19:50:34 +00001529// getIndexedType - Returns the type of the element that would be extracted
1530// with an extractvalue instruction with the specified parameters.
1531//
1532// A null type is returned if the indices are invalid for the specified
1533// pointer type.
1534//
1535const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001536 const unsigned *Idxs,
Dan Gohman12fce772008-05-15 19:50:34 +00001537 unsigned NumIdx) {
1538 unsigned CurIdx = 0;
1539 for (; CurIdx != NumIdx; ++CurIdx) {
1540 const CompositeType *CT = dyn_cast<CompositeType>(Agg);
Duncan Sands19d0b472010-02-16 11:11:14 +00001541 if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001542 unsigned Index = Idxs[CurIdx];
Dan Gohman12fce772008-05-15 19:50:34 +00001543 if (!CT->indexValid(Index)) return 0;
1544 Agg = CT->getTypeAtIndex(Index);
1545
1546 // If the new type forwards to another type, then it is in the middle
1547 // of being refined to another type (and hence, may have dropped all
1548 // references to what it was using before). So, use the new forwarded
1549 // type.
1550 if (const Type *Ty = Agg->getForwardedType())
1551 Agg = Ty;
1552 }
1553 return CurIdx == NumIdx ? Agg : 0;
1554}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001555
Dan Gohman4a3125b2008-06-17 21:07:55 +00001556const Type* ExtractValueInst::getIndexedType(const Type *Agg,
Dan Gohmand87e8132008-06-20 00:47:44 +00001557 unsigned Idx) {
1558 return getIndexedType(Agg, &Idx, 1);
Dan Gohman4a3125b2008-06-17 21:07:55 +00001559}
1560
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001561//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001562// BinaryOperator Class
1563//===----------------------------------------------------------------------===//
1564
Chris Lattner2195fc42007-02-24 00:55:48 +00001565BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001566 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001567 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001568 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001569 OperandTraits<BinaryOperator>::op_begin(this),
1570 OperandTraits<BinaryOperator>::operands(this),
1571 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001572 Op<0>() = S1;
1573 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001574 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001575 setName(Name);
1576}
1577
1578BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001579 const Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001580 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001581 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001582 OperandTraits<BinaryOperator>::op_begin(this),
1583 OperandTraits<BinaryOperator>::operands(this),
1584 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001585 Op<0>() = S1;
1586 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001587 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001588 setName(Name);
1589}
1590
1591
1592void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001593 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001594 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001595 assert(LHS->getType() == RHS->getType() &&
1596 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001597#ifndef NDEBUG
1598 switch (iType) {
1599 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001600 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001601 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001602 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001603 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001604 "Tried to create an integer operation on a non-integer type!");
1605 break;
1606 case FAdd: case FSub:
1607 case FMul:
1608 assert(getType() == LHS->getType() &&
1609 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001610 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001611 "Tried to create a floating-point operation on a "
1612 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001613 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001614 case UDiv:
1615 case SDiv:
1616 assert(getType() == LHS->getType() &&
1617 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001618 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001619 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001620 "Incorrect operand type (not integer) for S/UDIV");
1621 break;
1622 case FDiv:
1623 assert(getType() == LHS->getType() &&
1624 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001625 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001626 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001627 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001628 case URem:
1629 case SRem:
1630 assert(getType() == LHS->getType() &&
1631 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001632 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001633 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001634 "Incorrect operand type (not integer) for S/UREM");
1635 break;
1636 case FRem:
1637 assert(getType() == LHS->getType() &&
1638 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001639 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001640 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001641 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001642 case Shl:
1643 case LShr:
1644 case AShr:
1645 assert(getType() == LHS->getType() &&
1646 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001647 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001648 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001649 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001650 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001651 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001652 case And: case Or:
1653 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001654 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001655 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001656 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001657 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001658 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001659 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001660 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001661 default:
1662 break;
1663 }
1664#endif
1665}
1666
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001667BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001668 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001669 Instruction *InsertBefore) {
1670 assert(S1->getType() == S2->getType() &&
1671 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001672 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001673}
1674
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001675BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001676 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001677 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00001678 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001679 InsertAtEnd->getInstList().push_back(Res);
1680 return Res;
1681}
1682
Dan Gohman5476cfd2009-08-12 16:23:25 +00001683BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001684 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001685 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001686 return new BinaryOperator(Instruction::Sub,
1687 zero, Op,
1688 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001689}
1690
Dan Gohman5476cfd2009-08-12 16:23:25 +00001691BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001692 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001693 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00001694 return new BinaryOperator(Instruction::Sub,
1695 zero, Op,
1696 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001697}
1698
Dan Gohman4ab44202009-12-18 02:58:50 +00001699BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1700 Instruction *InsertBefore) {
1701 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1702 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
1703}
1704
1705BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
1706 BasicBlock *InsertAtEnd) {
1707 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1708 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
1709}
1710
Duncan Sandsfa5f5962010-02-02 12:53:04 +00001711BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1712 Instruction *InsertBefore) {
1713 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1714 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
1715}
1716
1717BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
1718 BasicBlock *InsertAtEnd) {
1719 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
1720 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
1721}
1722
Dan Gohman5476cfd2009-08-12 16:23:25 +00001723BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001724 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001725 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001726 return new BinaryOperator(Instruction::FSub,
1727 zero, Op,
1728 Op->getType(), Name, InsertBefore);
1729}
1730
Dan Gohman5476cfd2009-08-12 16:23:25 +00001731BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00001732 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00001733 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Dan Gohmana5b96452009-06-04 22:49:04 +00001734 return new BinaryOperator(Instruction::FSub,
1735 zero, Op,
1736 Op->getType(), Name, InsertAtEnd);
1737}
1738
Dan Gohman5476cfd2009-08-12 16:23:25 +00001739BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001740 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001741 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001742 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001743 C = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001744 C = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001745 std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001746 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001747 C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001748 }
1749
1750 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001751 Op->getType(), Name, InsertBefore);
1752}
1753
Dan Gohman5476cfd2009-08-12 16:23:25 +00001754BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001755 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001756 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001757 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001758 // Create a vector of all ones values.
Owen Anderson5a1acd92009-07-31 20:28:14 +00001759 Constant *Elt = Constant::getAllOnesValue(PTy->getElementType());
Owen Anderson4aa32952009-07-28 21:19:26 +00001760 AllOnes = ConstantVector::get(
Owen Andersonf945a9e2009-07-15 21:51:10 +00001761 std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001762 } else {
Owen Anderson5a1acd92009-07-31 20:28:14 +00001763 AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001764 }
1765
1766 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001767 Op->getType(), Name, InsertAtEnd);
1768}
1769
1770
1771// isConstantAllOnes - Helper function for several functions below
1772static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001773 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1774 return CI->isAllOnesValue();
1775 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1776 return CV->isAllOnesValue();
1777 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001778}
1779
Owen Andersonbb2501b2009-07-13 22:18:28 +00001780bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001781 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1782 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001783 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
1784 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001785 return false;
1786}
1787
Owen Andersonbb2501b2009-07-13 22:18:28 +00001788bool BinaryOperator::isFNeg(const Value *V) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001789 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1790 if (Bop->getOpcode() == Instruction::FSub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00001791 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
Dan Gohman11ff5702009-09-11 00:05:10 +00001792 return C->isNegativeZeroValue();
Dan Gohmana5b96452009-06-04 22:49:04 +00001793 return false;
1794}
1795
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001796bool BinaryOperator::isNot(const Value *V) {
1797 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1798 return (Bop->getOpcode() == Instruction::Xor &&
1799 (isConstantAllOnes(Bop->getOperand(1)) ||
1800 isConstantAllOnes(Bop->getOperand(0))));
1801 return false;
1802}
1803
Chris Lattner2c7d1772005-04-24 07:28:37 +00001804Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00001805 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001806}
1807
Chris Lattner2c7d1772005-04-24 07:28:37 +00001808const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1809 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001810}
1811
Dan Gohmana5b96452009-06-04 22:49:04 +00001812Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00001813 return cast<BinaryOperator>(BinOp)->getOperand(1);
1814}
1815
1816const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
1817 return getFNegArgument(const_cast<Value*>(BinOp));
1818}
1819
Chris Lattner2c7d1772005-04-24 07:28:37 +00001820Value *BinaryOperator::getNotArgument(Value *BinOp) {
1821 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1822 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1823 Value *Op0 = BO->getOperand(0);
1824 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001825 if (isConstantAllOnes(Op0)) return Op1;
1826
1827 assert(isConstantAllOnes(Op1));
1828 return Op0;
1829}
1830
Chris Lattner2c7d1772005-04-24 07:28:37 +00001831const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1832 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001833}
1834
1835
1836// swapOperands - Exchange the two operands to this instruction. This
1837// instruction is safe to use on any binary instruction and does not
1838// modify the semantics of the instruction. If the instruction is
1839// order dependent (SetLT f.e.) the opcode is changed.
1840//
1841bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001842 if (!isCommutative())
1843 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00001844 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001845 return false;
1846}
1847
Dan Gohman1b849082009-09-07 23:54:19 +00001848void BinaryOperator::setHasNoUnsignedWrap(bool b) {
1849 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
1850}
1851
1852void BinaryOperator::setHasNoSignedWrap(bool b) {
1853 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
1854}
1855
1856void BinaryOperator::setIsExact(bool b) {
1857 cast<SDivOperator>(this)->setIsExact(b);
1858}
1859
Nick Lewycky28a5f252009-09-27 21:33:04 +00001860bool BinaryOperator::hasNoUnsignedWrap() const {
1861 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
1862}
1863
1864bool BinaryOperator::hasNoSignedWrap() const {
1865 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
1866}
1867
1868bool BinaryOperator::isExact() const {
1869 return cast<SDivOperator>(this)->isExact();
1870}
1871
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001872//===----------------------------------------------------------------------===//
1873// CastInst Class
1874//===----------------------------------------------------------------------===//
1875
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001876// Just determine if this cast only deals with integral->integral conversion.
1877bool CastInst::isIntegerCast() const {
1878 switch (getOpcode()) {
1879 default: return false;
1880 case Instruction::ZExt:
1881 case Instruction::SExt:
1882 case Instruction::Trunc:
1883 return true;
1884 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00001885 return getOperand(0)->getType()->isIntegerTy() &&
1886 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001888}
1889
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001890bool CastInst::isLosslessCast() const {
1891 // Only BitCast can be lossless, exit fast if we're not BitCast
1892 if (getOpcode() != Instruction::BitCast)
1893 return false;
1894
1895 // Identity cast is always lossless
1896 const Type* SrcTy = getOperand(0)->getType();
1897 const Type* DstTy = getType();
1898 if (SrcTy == DstTy)
1899 return true;
1900
Reid Spencer8d9336d2006-12-31 05:26:44 +00001901 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00001902 if (SrcTy->isPointerTy())
1903 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001904 return false; // Other types have no identity values
1905}
1906
1907/// This function determines if the CastInst does not require any bits to be
1908/// changed in order to effect the cast. Essentially, it identifies cases where
1909/// no code gen is necessary for the cast, hence the name no-op cast. For
1910/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00001911/// # bitcast i32* %x to i8*
1912/// # bitcast <2 x i32> %x to <4 x i16>
1913/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001914/// @brief Determine if a cast is a no-op.
1915bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1916 switch (getOpcode()) {
1917 default:
1918 assert(!"Invalid CastOp");
1919 case Instruction::Trunc:
1920 case Instruction::ZExt:
1921 case Instruction::SExt:
1922 case Instruction::FPTrunc:
1923 case Instruction::FPExt:
1924 case Instruction::UIToFP:
1925 case Instruction::SIToFP:
1926 case Instruction::FPToUI:
1927 case Instruction::FPToSI:
1928 return false; // These always modify bits
1929 case Instruction::BitCast:
1930 return true; // BitCast never modifies bits.
1931 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001932 return IntPtrTy->getScalarSizeInBits() ==
1933 getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001934 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00001935 return IntPtrTy->getScalarSizeInBits() ==
1936 getOperand(0)->getType()->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001937 }
1938}
1939
1940/// This function determines if a pair of casts can be eliminated and what
1941/// opcode should be used in the elimination. This assumes that there are two
1942/// instructions like this:
1943/// * %F = firstOpcode SrcTy %x to MidTy
1944/// * %S = secondOpcode MidTy %F to DstTy
1945/// The function returns a resultOpcode so these two casts can be replaced with:
1946/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1947/// If no such cast is permited, the function returns 0.
1948unsigned CastInst::isEliminableCastPair(
1949 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1950 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1951{
1952 // Define the 144 possibilities for these two cast instructions. The values
1953 // in this matrix determine what to do in a given situation and select the
1954 // case in the switch below. The rows correspond to firstOp, the columns
1955 // correspond to secondOp. In looking at the table below, keep in mind
1956 // the following cast properties:
1957 //
1958 // Size Compare Source Destination
1959 // Operator Src ? Size Type Sign Type Sign
1960 // -------- ------------ ------------------- ---------------------
1961 // TRUNC > Integer Any Integral Any
1962 // ZEXT < Integral Unsigned Integer Any
1963 // SEXT < Integral Signed Integer Any
1964 // FPTOUI n/a FloatPt n/a Integral Unsigned
1965 // FPTOSI n/a FloatPt n/a Integral Signed
1966 // UITOFP n/a Integral Unsigned FloatPt n/a
1967 // SITOFP n/a Integral Signed FloatPt n/a
1968 // FPTRUNC > FloatPt n/a FloatPt n/a
1969 // FPEXT < FloatPt n/a FloatPt n/a
1970 // PTRTOINT n/a Pointer n/a Integral Unsigned
1971 // INTTOPTR n/a Integral Unsigned Pointer n/a
Dan Gohmaneb7111b2010-04-07 23:22:42 +00001972 // BITCAST = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001973 //
1974 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00001975 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
1976 // into "fptoui double to i64", but this loses information about the range
Chris Lattner6f6b4972006-12-05 23:43:59 +00001977 // of the produced value (we no longer know the top-part is all zeros).
1978 // Further this conversion is often much more expensive for typical hardware,
1979 // and causes issues when building libgcc. We disallow fptosi+sext for the
1980 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001981 const unsigned numCastOps =
1982 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1983 static const uint8_t CastResults[numCastOps][numCastOps] = {
1984 // T F F U S F F P I B -+
1985 // R Z S P P I I T P 2 N T |
1986 // U E E 2 2 2 2 R E I T C +- secondOp
1987 // N X X U S F F N X N 2 V |
1988 // C T T I I P P C T T P T -+
1989 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1990 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1991 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001992 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1993 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001994 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1995 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1996 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1997 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1998 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1999 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
2000 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
2001 };
2002
2003 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2004 [secondOp-Instruction::CastOpsBegin];
2005 switch (ElimCase) {
2006 case 0:
2007 // categorically disallowed
2008 return 0;
2009 case 1:
2010 // allowed, use first cast's opcode
2011 return firstOp;
2012 case 2:
2013 // allowed, use second cast's opcode
2014 return secondOp;
2015 case 3:
2016 // no-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002017 // is integer and we are not converting between a vector and a
Chris Lattner531732b2010-01-23 04:42:42 +00002018 // non vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002019 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002020 return firstOp;
2021 return 0;
2022 case 4:
2023 // no-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002024 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002025 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026 return firstOp;
2027 return 0;
2028 case 5:
2029 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002030 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002031 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032 return secondOp;
2033 return 0;
2034 case 6:
2035 // no-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002036 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002037 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038 return secondOp;
2039 return 0;
2040 case 7: {
2041 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
Dan Gohman9413de12009-07-21 23:19:40 +00002042 if (!IntPtrTy)
2043 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002044 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2045 unsigned MidSize = MidTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046 if (MidSize >= PtrSize)
2047 return Instruction::BitCast;
2048 return 0;
2049 }
2050 case 8: {
2051 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2052 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2053 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002054 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2055 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002056 if (SrcSize == DstSize)
2057 return Instruction::BitCast;
2058 else if (SrcSize < DstSize)
2059 return firstOp;
2060 return secondOp;
2061 }
2062 case 9: // zext, sext -> zext, because sext can't sign extend after zext
2063 return Instruction::ZExt;
2064 case 10:
2065 // fpext followed by ftrunc is allowed if the bit size returned to is
2066 // the same as the original, in which case its just a bitcast
2067 if (SrcTy == DstTy)
2068 return Instruction::BitCast;
2069 return 0; // If the types are not the same we can't eliminate it.
2070 case 11:
2071 // bitcast followed by ptrtoint is allowed as long as the bitcast
2072 // is a pointer to pointer cast.
Duncan Sands19d0b472010-02-16 11:11:14 +00002073 if (SrcTy->isPointerTy() && MidTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074 return secondOp;
2075 return 0;
2076 case 12:
2077 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
Duncan Sands19d0b472010-02-16 11:11:14 +00002078 if (MidTy->isPointerTy() && DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002079 return firstOp;
2080 return 0;
2081 case 13: {
2082 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Dan Gohman9413de12009-07-21 23:19:40 +00002083 if (!IntPtrTy)
2084 return 0;
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002085 unsigned PtrSize = IntPtrTy->getScalarSizeInBits();
2086 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2087 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002088 if (SrcSize <= PtrSize && SrcSize == DstSize)
2089 return Instruction::BitCast;
2090 return 0;
2091 }
2092 case 99:
2093 // cast combination can't happen (error in input). This is for all cases
2094 // where the MidTy is not the same for the two cast instructions.
2095 assert(!"Invalid Cast Combination");
2096 return 0;
2097 default:
2098 assert(!"Error in CastResults table!!!");
2099 return 0;
2100 }
2101 return 0;
2102}
2103
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002104CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002105 const Twine &Name, Instruction *InsertBefore) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002106 // Construct and return the appropriate CastInst subclass
2107 switch (op) {
2108 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2109 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2110 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2111 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2112 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2113 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2114 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2115 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2116 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2117 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2118 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2119 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2120 default:
2121 assert(!"Invalid opcode provided");
2122 }
2123 return 0;
2124}
2125
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002126CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002127 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002128 // Construct and return the appropriate CastInst subclass
2129 switch (op) {
2130 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2131 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2132 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2133 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2134 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2135 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2136 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2137 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2138 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2139 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2140 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2141 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2142 default:
2143 assert(!"Invalid opcode provided");
2144 }
2145 return 0;
2146}
2147
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002148CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002149 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002150 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002151 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002152 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2153 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002154}
2155
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002156CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002157 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002158 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002159 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002160 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2161 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002162}
2163
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002164CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002165 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002166 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002167 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002168 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2169 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002170}
2171
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002172CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002173 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002174 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002175 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002176 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2177 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002178}
2179
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002180CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002181 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002182 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002183 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002184 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2185 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002186}
2187
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002188CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002189 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002190 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002191 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002192 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2193 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002194}
2195
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002196CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002197 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002198 BasicBlock *InsertAtEnd) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002199 assert(S->getType()->isPointerTy() && "Invalid cast");
2200 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002201 "Invalid cast");
2202
Duncan Sands9dff9be2010-02-15 16:12:20 +00002203 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002204 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
2205 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002206}
2207
2208/// @brief Create a BitCast or a PtrToInt cast instruction
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002209CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002210 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002211 Instruction *InsertBefore) {
Duncan Sands19d0b472010-02-16 11:11:14 +00002212 assert(S->getType()->isPointerTy() && "Invalid cast");
2213 assert((Ty->isIntegerTy() || Ty->isPointerTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002214 "Invalid cast");
2215
Duncan Sands9dff9be2010-02-15 16:12:20 +00002216 if (Ty->isIntegerTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002217 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2218 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002219}
2220
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002221CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002222 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002223 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002224 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002225 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002226 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2227 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002228 Instruction::CastOps opcode =
2229 (SrcBits == DstBits ? Instruction::BitCast :
2230 (SrcBits > DstBits ? Instruction::Trunc :
2231 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002232 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002233}
2234
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002235CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002236 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002237 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002238 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002239 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002240 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2241 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002242 Instruction::CastOps opcode =
2243 (SrcBits == DstBits ? Instruction::BitCast :
2244 (SrcBits > DstBits ? Instruction::Trunc :
2245 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002246 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002247}
2248
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002249CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002250 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002251 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002252 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002253 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002254 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2255 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002256 Instruction::CastOps opcode =
2257 (SrcBits == DstBits ? Instruction::BitCast :
2258 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002259 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002260}
2261
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002262CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002263 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002264 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002265 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002266 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002267 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2268 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002269 Instruction::CastOps opcode =
2270 (SrcBits == DstBits ? Instruction::BitCast :
2271 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002272 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002273}
2274
Duncan Sands55e50902008-01-06 10:12:28 +00002275// Check whether it is valid to call getCastOpcode for these types.
2276// This routine must be kept in sync with getCastOpcode.
2277bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
2278 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2279 return false;
2280
2281 if (SrcTy == DestTy)
2282 return true;
2283
2284 // Get the bit sizes, we'll need these
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002285 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2286 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Duncan Sands55e50902008-01-06 10:12:28 +00002287
2288 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002289 if (DestTy->isIntegerTy()) { // Casting to integral
2290 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002291 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002292 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002293 return true;
2294 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002295 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002296 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002297 } else { // Casting from something else
Duncan Sands19d0b472010-02-16 11:11:14 +00002298 return SrcTy->isPointerTy();
Duncan Sands55e50902008-01-06 10:12:28 +00002299 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002300 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2301 if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002302 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002303 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Duncan Sands55e50902008-01-06 10:12:28 +00002304 return true;
2305 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002306 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002307 return DestBits == PTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002308 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002309 return false;
2310 }
2311 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002312 // Casting to vector
Duncan Sands55e50902008-01-06 10:12:28 +00002313 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Gabor Greif697e94c2008-05-15 10:04:30 +00002314 // Casting from vector
Duncan Sands55e50902008-01-06 10:12:28 +00002315 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
Gabor Greif697e94c2008-05-15 10:04:30 +00002316 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002317 return DestPTy->getBitWidth() == SrcBits;
2318 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002319 } else if (DestTy->isPointerTy()) { // Casting to pointer
2320 if (SrcTy->isPointerTy()) { // Casting from pointer
Duncan Sands55e50902008-01-06 10:12:28 +00002321 return true;
Duncan Sands9dff9be2010-02-15 16:12:20 +00002322 } else if (SrcTy->isIntegerTy()) { // Casting from integral
Duncan Sands55e50902008-01-06 10:12:28 +00002323 return true;
Gabor Greif697e94c2008-05-15 10:04:30 +00002324 } else { // Casting from something else
Duncan Sands55e50902008-01-06 10:12:28 +00002325 return false;
2326 }
Gabor Greif697e94c2008-05-15 10:04:30 +00002327 } else { // Casting to something else
Duncan Sands55e50902008-01-06 10:12:28 +00002328 return false;
2329 }
2330}
2331
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002332// Provide a way to get a "cast" where the cast opcode is inferred from the
2333// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002334// logic in the castIsValid function below. This axiom should hold:
2335// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2336// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002337// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002338// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002339Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002340CastInst::getCastOpcode(
2341 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002342 // Get the bit sizes, we'll need these
2343 const Type *SrcTy = Src->getType();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002344 unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr
2345 unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002346
Duncan Sands55e50902008-01-06 10:12:28 +00002347 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2348 "Only first class types are castable!");
2349
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002350 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002351 if (DestTy->isIntegerTy()) { // Casting to integral
2352 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002353 if (DestBits < SrcBits)
2354 return Trunc; // int -> smaller int
2355 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002356 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002357 return SExt; // signed -> SEXT
2358 else
2359 return ZExt; // unsigned -> ZEXT
2360 } else {
2361 return BitCast; // Same size, No-op cast
2362 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002363 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002364 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002365 return FPToSI; // FP -> sint
2366 else
2367 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00002368 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002370 "Casting vector to integer of different width");
Devang Patele9432132008-11-05 01:37:40 +00002371 PTy = NULL;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002372 return BitCast; // Same size, no-op cast
2373 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002374 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002375 "Casting from a value that is not first-class type");
2376 return PtrToInt; // ptr -> int
2377 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002378 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2379 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002380 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002381 return SIToFP; // sint -> FP
2382 else
2383 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002384 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002385 if (DestBits < SrcBits) {
2386 return FPTrunc; // FP -> smaller FP
2387 } else if (DestBits > SrcBits) {
2388 return FPExt; // FP -> larger FP
2389 } else {
2390 return BitCast; // same size, no-op cast
2391 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002392 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002393 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002394 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002395 PTy = NULL;
2396 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002397 } else {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002398 llvm_unreachable("Casting pointer or non-first class to float");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002399 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002400 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2401 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002403 "Casting vector to vector of different widths");
Devang Patelcb181bb2008-11-21 20:00:59 +00002404 SrcPTy = NULL;
Dan Gohmanfead7972007-05-11 21:43:24 +00002405 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002406 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002407 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002408 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002409 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002410 }
Duncan Sands19d0b472010-02-16 11:11:14 +00002411 } else if (DestTy->isPointerTy()) {
2412 if (SrcTy->isPointerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002413 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002414 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002415 return IntToPtr; // int -> ptr
2416 } else {
2417 assert(!"Casting pointer to other than pointer or int");
2418 }
2419 } else {
2420 assert(!"Casting to type that is not first-class");
2421 }
2422
2423 // If we fall through to here we probably hit an assertion cast above
2424 // and assertions are not turned on. Anything we return is an error, so
2425 // BitCast is as good a choice as any.
2426 return BitCast;
2427}
2428
2429//===----------------------------------------------------------------------===//
2430// CastInst SubClass Constructors
2431//===----------------------------------------------------------------------===//
2432
2433/// Check that the construction parameters for a CastInst are correct. This
2434/// could be broken out into the separate constructors but it is useful to have
2435/// it in one place and to eliminate the redundant code for getting the sizes
2436/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002437bool
2438CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002439
2440 // Check for type sanity on the arguments
2441 const Type *SrcTy = S->getType();
Chris Lattner37bc78a2010-01-26 21:51:43 +00002442 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
2443 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002444 return false;
2445
2446 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002447 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
2448 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002449
2450 // Switch on the opcode provided
2451 switch (op) {
2452 default: return false; // This is an input error
2453 case Instruction::Trunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002454 return SrcTy->isIntOrIntVectorTy() &&
2455 DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002456 case Instruction::ZExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002457 return SrcTy->isIntOrIntVectorTy() &&
2458 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002459 case Instruction::SExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002460 return SrcTy->isIntOrIntVectorTy() &&
2461 DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002462 case Instruction::FPTrunc:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002463 return SrcTy->isFPOrFPVectorTy() &&
2464 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002465 SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002466 case Instruction::FPExt:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002467 return SrcTy->isFPOrFPVectorTy() &&
2468 DstTy->isFPOrFPVectorTy() &&
Dan Gohman550c9af2008-08-14 20:04:46 +00002469 SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002470 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002471 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002472 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2473 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002474 return SVTy->getElementType()->isIntOrIntVectorTy() &&
2475 DVTy->getElementType()->isFPOrFPVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002476 SVTy->getNumElements() == DVTy->getNumElements();
2477 }
2478 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002479 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002480 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002481 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002482 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2483 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002484 return SVTy->getElementType()->isFPOrFPVectorTy() &&
2485 DVTy->getElementType()->isIntOrIntVectorTy() &&
Nate Begemand4d45c22007-11-17 03:58:34 +00002486 SVTy->getNumElements() == DVTy->getNumElements();
2487 }
2488 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002489 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490 case Instruction::PtrToInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00002491 return SrcTy->isPointerTy() && DstTy->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002492 case Instruction::IntToPtr:
Duncan Sands19d0b472010-02-16 11:11:14 +00002493 return SrcTy->isIntegerTy() && DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002494 case Instruction::BitCast:
2495 // BitCast implies a no-op cast of type only. No bits change.
2496 // However, you can't cast pointers to anything but pointers.
Duncan Sands19d0b472010-02-16 11:11:14 +00002497 if (SrcTy->isPointerTy() != DstTy->isPointerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 return false;
2499
Duncan Sands55e50902008-01-06 10:12:28 +00002500 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002501 // these cases, the cast is okay if the source and destination bit widths
2502 // are identical.
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002503 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504 }
2505}
2506
2507TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002508 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002509) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002510 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002511}
2512
2513TruncInst::TruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002514 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002515) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002516 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002517}
2518
2519ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002520 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002521) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002522 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002523}
2524
2525ZExtInst::ZExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002526 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002527) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002528 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002529}
2530SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002531 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002532) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002533 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002534}
2535
Jeff Cohencc08c832006-12-02 02:22:01 +00002536SExtInst::SExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002537 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002538) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002539 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002540}
2541
2542FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002543 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002544) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002545 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002546}
2547
2548FPTruncInst::FPTruncInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002549 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002550) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002551 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002552}
2553
2554FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002555 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002556) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002557 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002558}
2559
2560FPExtInst::FPExtInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002561 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002563 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564}
2565
2566UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002567 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002569 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002570}
2571
2572UIToFPInst::UIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002573 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002575 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002576}
2577
2578SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002579 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002580) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002581 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002582}
2583
2584SIToFPInst::SIToFPInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002585 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002586) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002587 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002588}
2589
2590FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002591 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002592) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002593 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002594}
2595
2596FPToUIInst::FPToUIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002597 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002598) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002599 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002600}
2601
2602FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002603 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002604) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002605 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002606}
2607
2608FPToSIInst::FPToSIInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002609 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002610) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002611 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612}
2613
2614PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002615 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002616) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002617 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002618}
2619
2620PtrToIntInst::PtrToIntInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002621 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002622) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002623 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002624}
2625
2626IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002627 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002628) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002629 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002630}
2631
2632IntToPtrInst::IntToPtrInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002633 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002634) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002635 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002636}
2637
2638BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002639 Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002640) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002641 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002642}
2643
2644BitCastInst::BitCastInst(
Daniel Dunbar4975db62009-07-25 04:41:11 +00002645 Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002646) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002647 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002648}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002649
2650//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002651// CmpInst Classes
2652//===----------------------------------------------------------------------===//
2653
Chris Lattneraec33da2010-01-22 06:25:37 +00002654void CmpInst::Anchor() const {}
2655
Nate Begemand2195702008-05-12 19:01:56 +00002656CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002657 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002658 Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002659 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002660 OperandTraits<CmpInst>::op_begin(this),
2661 OperandTraits<CmpInst>::operands(this),
2662 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002663 Op<0>() = LHS;
2664 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002665 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002666 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002667}
Gabor Greiff6caff662008-05-10 08:32:32 +00002668
Nate Begemand2195702008-05-12 19:01:56 +00002669CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002670 Value *LHS, Value *RHS, const Twine &Name,
Nate Begemand2195702008-05-12 19:01:56 +00002671 BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00002672 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00002673 OperandTraits<CmpInst>::op_begin(this),
2674 OperandTraits<CmpInst>::operands(this),
2675 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002676 Op<0>() = LHS;
2677 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00002678 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00002679 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002680}
2681
2682CmpInst *
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002683CmpInst::Create(OtherOps Op, unsigned short predicate,
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002684 Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002685 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002686 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002687 if (InsertBefore)
2688 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
2689 S1, S2, Name);
2690 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002691 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002692 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002693 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002694
2695 if (InsertBefore)
2696 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
2697 S1, S2, Name);
2698 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00002699 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002700 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002701}
2702
2703CmpInst *
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002704CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002705 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002706 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002707 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2708 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002709 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00002710 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
2711 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002712}
2713
2714void CmpInst::swapOperands() {
2715 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2716 IC->swapOperands();
2717 else
2718 cast<FCmpInst>(this)->swapOperands();
2719}
2720
2721bool CmpInst::isCommutative() {
2722 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2723 return IC->isCommutative();
2724 return cast<FCmpInst>(this)->isCommutative();
2725}
2726
2727bool CmpInst::isEquality() {
2728 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2729 return IC->isEquality();
2730 return cast<FCmpInst>(this)->isEquality();
2731}
2732
2733
Dan Gohman4e724382008-05-31 02:47:54 +00002734CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002735 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002736 default: assert(!"Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002737 case ICMP_EQ: return ICMP_NE;
2738 case ICMP_NE: return ICMP_EQ;
2739 case ICMP_UGT: return ICMP_ULE;
2740 case ICMP_ULT: return ICMP_UGE;
2741 case ICMP_UGE: return ICMP_ULT;
2742 case ICMP_ULE: return ICMP_UGT;
2743 case ICMP_SGT: return ICMP_SLE;
2744 case ICMP_SLT: return ICMP_SGE;
2745 case ICMP_SGE: return ICMP_SLT;
2746 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00002747
Dan Gohman4e724382008-05-31 02:47:54 +00002748 case FCMP_OEQ: return FCMP_UNE;
2749 case FCMP_ONE: return FCMP_UEQ;
2750 case FCMP_OGT: return FCMP_ULE;
2751 case FCMP_OLT: return FCMP_UGE;
2752 case FCMP_OGE: return FCMP_ULT;
2753 case FCMP_OLE: return FCMP_UGT;
2754 case FCMP_UEQ: return FCMP_ONE;
2755 case FCMP_UNE: return FCMP_OEQ;
2756 case FCMP_UGT: return FCMP_OLE;
2757 case FCMP_ULT: return FCMP_OGE;
2758 case FCMP_UGE: return FCMP_OLT;
2759 case FCMP_ULE: return FCMP_OGT;
2760 case FCMP_ORD: return FCMP_UNO;
2761 case FCMP_UNO: return FCMP_ORD;
2762 case FCMP_TRUE: return FCMP_FALSE;
2763 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00002764 }
2765}
2766
Reid Spencer266e42b2006-12-23 06:05:41 +00002767ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2768 switch (pred) {
2769 default: assert(! "Unknown icmp predicate!");
2770 case ICMP_EQ: case ICMP_NE:
2771 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2772 return pred;
2773 case ICMP_UGT: return ICMP_SGT;
2774 case ICMP_ULT: return ICMP_SLT;
2775 case ICMP_UGE: return ICMP_SGE;
2776 case ICMP_ULE: return ICMP_SLE;
2777 }
2778}
2779
Nick Lewycky8ea81e82008-01-28 03:48:02 +00002780ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
2781 switch (pred) {
2782 default: assert(! "Unknown icmp predicate!");
2783 case ICMP_EQ: case ICMP_NE:
2784 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
2785 return pred;
2786 case ICMP_SGT: return ICMP_UGT;
2787 case ICMP_SLT: return ICMP_ULT;
2788 case ICMP_SGE: return ICMP_UGE;
2789 case ICMP_SLE: return ICMP_ULE;
2790 }
2791}
2792
Reid Spencer0286bc12007-02-28 22:00:54 +00002793/// Initialize a set of values that all satisfy the condition with C.
2794///
2795ConstantRange
2796ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2797 APInt Lower(C);
2798 APInt Upper(C);
2799 uint32_t BitWidth = C.getBitWidth();
2800 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002801 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Reid Spencer0286bc12007-02-28 22:00:54 +00002802 case ICmpInst::ICMP_EQ: Upper++; break;
2803 case ICmpInst::ICMP_NE: Lower++; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00002804 case ICmpInst::ICMP_ULT:
2805 Lower = APInt::getMinValue(BitWidth);
2806 // Check for an empty-set condition.
2807 if (Lower == Upper)
2808 return ConstantRange(BitWidth, /*isFullSet=*/false);
2809 break;
2810 case ICmpInst::ICMP_SLT:
2811 Lower = APInt::getSignedMinValue(BitWidth);
2812 // Check for an empty-set condition.
2813 if (Lower == Upper)
2814 return ConstantRange(BitWidth, /*isFullSet=*/false);
2815 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00002816 case ICmpInst::ICMP_UGT:
2817 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002818 // Check for an empty-set condition.
2819 if (Lower == Upper)
2820 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002821 break;
2822 case ICmpInst::ICMP_SGT:
2823 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002824 // Check for an empty-set condition.
2825 if (Lower == Upper)
2826 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00002827 break;
2828 case ICmpInst::ICMP_ULE:
2829 Lower = APInt::getMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002830 // Check for a full-set condition.
2831 if (Lower == Upper)
2832 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002833 break;
2834 case ICmpInst::ICMP_SLE:
2835 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
Dan Gohmand86e2952010-01-26 16:04:20 +00002836 // Check for a full-set condition.
2837 if (Lower == Upper)
2838 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002839 break;
2840 case ICmpInst::ICMP_UGE:
2841 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002842 // Check for a full-set condition.
2843 if (Lower == Upper)
2844 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002845 break;
2846 case ICmpInst::ICMP_SGE:
2847 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00002848 // Check for a full-set condition.
2849 if (Lower == Upper)
2850 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00002851 break;
2852 }
2853 return ConstantRange(Lower, Upper);
2854}
2855
Dan Gohman4e724382008-05-31 02:47:54 +00002856CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002857 switch (pred) {
Dan Gohman4e724382008-05-31 02:47:54 +00002858 default: assert(!"Unknown cmp predicate!");
2859 case ICMP_EQ: case ICMP_NE:
2860 return pred;
2861 case ICMP_SGT: return ICMP_SLT;
2862 case ICMP_SLT: return ICMP_SGT;
2863 case ICMP_SGE: return ICMP_SLE;
2864 case ICMP_SLE: return ICMP_SGE;
2865 case ICMP_UGT: return ICMP_ULT;
2866 case ICMP_ULT: return ICMP_UGT;
2867 case ICMP_UGE: return ICMP_ULE;
2868 case ICMP_ULE: return ICMP_UGE;
2869
Reid Spencerd9436b62006-11-20 01:22:35 +00002870 case FCMP_FALSE: case FCMP_TRUE:
2871 case FCMP_OEQ: case FCMP_ONE:
2872 case FCMP_UEQ: case FCMP_UNE:
2873 case FCMP_ORD: case FCMP_UNO:
2874 return pred;
2875 case FCMP_OGT: return FCMP_OLT;
2876 case FCMP_OLT: return FCMP_OGT;
2877 case FCMP_OGE: return FCMP_OLE;
2878 case FCMP_OLE: return FCMP_OGE;
2879 case FCMP_UGT: return FCMP_ULT;
2880 case FCMP_ULT: return FCMP_UGT;
2881 case FCMP_UGE: return FCMP_ULE;
2882 case FCMP_ULE: return FCMP_UGE;
2883 }
2884}
2885
Reid Spencer266e42b2006-12-23 06:05:41 +00002886bool CmpInst::isUnsigned(unsigned short predicate) {
2887 switch (predicate) {
2888 default: return false;
2889 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2890 case ICmpInst::ICMP_UGE: return true;
2891 }
2892}
2893
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002894bool CmpInst::isSigned(unsigned short predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002895 switch (predicate) {
2896 default: return false;
2897 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2898 case ICmpInst::ICMP_SGE: return true;
2899 }
2900}
2901
2902bool CmpInst::isOrdered(unsigned short predicate) {
2903 switch (predicate) {
2904 default: return false;
2905 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2906 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2907 case FCmpInst::FCMP_ORD: return true;
2908 }
2909}
2910
2911bool CmpInst::isUnordered(unsigned short predicate) {
2912 switch (predicate) {
2913 default: return false;
2914 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2915 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2916 case FCmpInst::FCMP_UNO: return true;
2917 }
2918}
2919
Nick Lewycky7494b3b2009-10-25 03:50:03 +00002920bool CmpInst::isTrueWhenEqual(unsigned short predicate) {
2921 switch(predicate) {
2922 default: return false;
2923 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
2924 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
2925 }
2926}
2927
2928bool CmpInst::isFalseWhenEqual(unsigned short predicate) {
2929 switch(predicate) {
2930 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
2931 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
2932 default: return false;
2933 }
2934}
2935
2936
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002937//===----------------------------------------------------------------------===//
2938// SwitchInst Implementation
2939//===----------------------------------------------------------------------===//
2940
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002941void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002942 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002943 ReservedSpace = 2+NumCases*2;
2944 NumOperands = 2;
Gabor Greiff6caff662008-05-10 08:32:32 +00002945 OperandList = allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002946
Gabor Greif2d3024d2008-05-26 21:33:52 +00002947 OperandList[0] = Value;
2948 OperandList[1] = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002949}
2950
Chris Lattner2195fc42007-02-24 00:55:48 +00002951/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2952/// switch on and a default destination. The number of additional cases can
2953/// be specified here to make memory allocation more efficient. This
2954/// constructor can also autoinsert before another instruction.
2955SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2956 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00002957 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2958 0, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002959 init(Value, Default, NumCases);
2960}
2961
2962/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2963/// switch on and a default destination. The number of additional cases can
2964/// be specified here to make memory allocation more efficient. This
2965/// constructor also autoinserts at the end of the specified BasicBlock.
2966SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2967 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00002968 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
2969 0, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +00002970 init(Value, Default, NumCases);
2971}
2972
Misha Brukmanb1c93172005-04-21 23:48:37 +00002973SwitchInst::SwitchInst(const SwitchInst &SI)
Owen Anderson55f1c092009-08-13 21:58:54 +00002974 : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch,
Gabor Greiff6caff662008-05-10 08:32:32 +00002975 allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002976 Use *OL = OperandList, *InOL = SI.OperandList;
2977 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00002978 OL[i] = InOL[i];
2979 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002980 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00002981 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002982}
2983
Gordon Henriksen14a55692007-12-10 02:14:30 +00002984SwitchInst::~SwitchInst() {
Gabor Greiff6caff662008-05-10 08:32:32 +00002985 dropHungoffUses(OperandList);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002986}
2987
2988
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002989/// addCase - Add an entry to the switch instruction...
2990///
Chris Lattner47ac1872005-02-24 05:32:09 +00002991void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002992 unsigned OpNo = NumOperands;
2993 if (OpNo+2 > ReservedSpace)
2994 resizeOperands(0); // Get more space!
2995 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002996 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002997 NumOperands = OpNo+2;
Gabor Greif2d3024d2008-05-26 21:33:52 +00002998 OperandList[OpNo] = OnVal;
2999 OperandList[OpNo+1] = Dest;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003000}
3001
3002/// removeCase - This method removes the specified successor from the switch
3003/// instruction. Note that this cannot be used to remove the default
3004/// destination (successor #0).
3005///
3006void SwitchInst::removeCase(unsigned idx) {
3007 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003008 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
3009
3010 unsigned NumOps = getNumOperands();
3011 Use *OL = OperandList;
3012
3013 // Move everything after this operand down.
3014 //
3015 // FIXME: we could just swap with the end of the list, then erase. However,
3016 // client might not expect this to happen. The code as it is thrashes the
3017 // use/def lists, which is kinda lame.
3018 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
3019 OL[i-2] = OL[i];
3020 OL[i-2+1] = OL[i+1];
3021 }
3022
3023 // Nuke the last value.
3024 OL[NumOps-2].set(0);
3025 OL[NumOps-2+1].set(0);
3026 NumOperands = NumOps-2;
3027}
3028
3029/// resizeOperands - resize operands - This adjusts the length of the operands
3030/// list according to the following behavior:
3031/// 1. If NumOps == 0, grow the operand list in response to a push_back style
Gabor Greiff6caff662008-05-10 08:32:32 +00003032/// of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003033/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3034/// 3. If NumOps == NumOperands, trim the reserved space.
3035///
3036void SwitchInst::resizeOperands(unsigned NumOps) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003037 unsigned e = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003038 if (NumOps == 0) {
Gabor Greiff6caff662008-05-10 08:32:32 +00003039 NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003040 } else if (NumOps*2 > NumOperands) {
3041 // No resize needed.
3042 if (ReservedSpace >= NumOps) return;
3043 } else if (NumOps == NumOperands) {
3044 if (ReservedSpace == NumOps) return;
3045 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003046 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003047 }
3048
3049 ReservedSpace = NumOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003050 Use *NewOps = allocHungoffUses(NumOps);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003051 Use *OldOps = OperandList;
Gabor Greiff6caff662008-05-10 08:32:32 +00003052 for (unsigned i = 0; i != e; ++i) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003053 NewOps[i] = OldOps[i];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003054 }
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003055 OperandList = NewOps;
Gabor Greiff6caff662008-05-10 08:32:32 +00003056 if (OldOps) Use::zap(OldOps, OldOps + e, true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003057}
3058
3059
3060BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3061 return getSuccessor(idx);
3062}
3063unsigned SwitchInst::getNumSuccessorsV() const {
3064 return getNumSuccessors();
3065}
3066void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3067 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003068}
Chris Lattnerf22be932004-10-15 23:52:53 +00003069
Chris Lattner3ed871f2009-10-27 19:13:16 +00003070//===----------------------------------------------------------------------===//
3071// SwitchInst Implementation
3072//===----------------------------------------------------------------------===//
3073
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003074void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003075 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003076 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003077 ReservedSpace = 1+NumDests;
3078 NumOperands = 1;
3079 OperandList = allocHungoffUses(ReservedSpace);
3080
3081 OperandList[0] = Address;
3082}
3083
3084
3085/// resizeOperands - resize operands - This adjusts the length of the operands
3086/// list according to the following behavior:
3087/// 1. If NumOps == 0, grow the operand list in response to a push_back style
3088/// of operation. This grows the number of ops by 2 times.
3089/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
3090/// 3. If NumOps == NumOperands, trim the reserved space.
3091///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003092void IndirectBrInst::resizeOperands(unsigned NumOps) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003093 unsigned e = getNumOperands();
3094 if (NumOps == 0) {
3095 NumOps = e*2;
3096 } else if (NumOps*2 > NumOperands) {
3097 // No resize needed.
3098 if (ReservedSpace >= NumOps) return;
3099 } else if (NumOps == NumOperands) {
3100 if (ReservedSpace == NumOps) return;
3101 } else {
3102 return;
3103 }
3104
3105 ReservedSpace = NumOps;
3106 Use *NewOps = allocHungoffUses(NumOps);
3107 Use *OldOps = OperandList;
3108 for (unsigned i = 0; i != e; ++i)
3109 NewOps[i] = OldOps[i];
3110 OperandList = NewOps;
3111 if (OldOps) Use::zap(OldOps, OldOps + e, true);
3112}
3113
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003114IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3115 Instruction *InsertBefore)
3116: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003117 0, 0, InsertBefore) {
3118 init(Address, NumCases);
3119}
3120
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003121IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3122 BasicBlock *InsertAtEnd)
3123: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003124 0, 0, InsertAtEnd) {
3125 init(Address, NumCases);
3126}
3127
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003128IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
3129 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
Chris Lattner3ed871f2009-10-27 19:13:16 +00003130 allocHungoffUses(IBI.getNumOperands()),
3131 IBI.getNumOperands()) {
3132 Use *OL = OperandList, *InOL = IBI.OperandList;
3133 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3134 OL[i] = InOL[i];
3135 SubclassOptionalData = IBI.SubclassOptionalData;
3136}
3137
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003138IndirectBrInst::~IndirectBrInst() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003139 dropHungoffUses(OperandList);
3140}
3141
3142/// addDestination - Add a destination.
3143///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003144void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003145 unsigned OpNo = NumOperands;
3146 if (OpNo+1 > ReservedSpace)
3147 resizeOperands(0); // Get more space!
3148 // Initialize some new operands.
3149 assert(OpNo < ReservedSpace && "Growing didn't work!");
3150 NumOperands = OpNo+1;
3151 OperandList[OpNo] = DestBB;
3152}
3153
3154/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003155/// indirectbr instruction.
3156void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003157 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3158
3159 unsigned NumOps = getNumOperands();
3160 Use *OL = OperandList;
3161
3162 // Replace this value with the last one.
3163 OL[idx+1] = OL[NumOps-1];
3164
3165 // Nuke the last value.
3166 OL[NumOps-1].set(0);
3167 NumOperands = NumOps-1;
3168}
3169
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003170BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003171 return getSuccessor(idx);
3172}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003173unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003174 return getNumSuccessors();
3175}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003176void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003177 setSuccessor(idx, B);
3178}
3179
3180//===----------------------------------------------------------------------===//
Devang Patel11cf3f42009-10-27 22:16:29 +00003181// clone_impl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003182//===----------------------------------------------------------------------===//
3183
Chris Lattnerf22be932004-10-15 23:52:53 +00003184// Define these methods here so vtables don't get emitted into every translation
3185// unit that uses these classes.
3186
Devang Patel11cf3f42009-10-27 22:16:29 +00003187GetElementPtrInst *GetElementPtrInst::clone_impl() const {
3188 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003189}
3190
Devang Patel11cf3f42009-10-27 22:16:29 +00003191BinaryOperator *BinaryOperator::clone_impl() const {
3192 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003193}
3194
Devang Patel11cf3f42009-10-27 22:16:29 +00003195FCmpInst* FCmpInst::clone_impl() const {
3196 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003197}
3198
Devang Patel11cf3f42009-10-27 22:16:29 +00003199ICmpInst* ICmpInst::clone_impl() const {
3200 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003201}
3202
Devang Patel11cf3f42009-10-27 22:16:29 +00003203ExtractValueInst *ExtractValueInst::clone_impl() const {
3204 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003205}
3206
Devang Patel11cf3f42009-10-27 22:16:29 +00003207InsertValueInst *InsertValueInst::clone_impl() const {
3208 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003209}
3210
Devang Patel11cf3f42009-10-27 22:16:29 +00003211AllocaInst *AllocaInst::clone_impl() const {
3212 return new AllocaInst(getAllocatedType(),
3213 (Value*)getOperand(0),
3214 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003215}
3216
Devang Patel11cf3f42009-10-27 22:16:29 +00003217LoadInst *LoadInst::clone_impl() const {
3218 return new LoadInst(getOperand(0),
3219 Twine(), isVolatile(),
3220 getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003221}
3222
Devang Patel11cf3f42009-10-27 22:16:29 +00003223StoreInst *StoreInst::clone_impl() const {
3224 return new StoreInst(getOperand(0), getOperand(1),
3225 isVolatile(), getAlignment());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003226}
3227
Devang Patel11cf3f42009-10-27 22:16:29 +00003228TruncInst *TruncInst::clone_impl() const {
3229 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003230}
3231
Devang Patel11cf3f42009-10-27 22:16:29 +00003232ZExtInst *ZExtInst::clone_impl() const {
3233 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003234}
3235
Devang Patel11cf3f42009-10-27 22:16:29 +00003236SExtInst *SExtInst::clone_impl() const {
3237 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003238}
3239
Devang Patel11cf3f42009-10-27 22:16:29 +00003240FPTruncInst *FPTruncInst::clone_impl() const {
3241 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003242}
3243
Devang Patel11cf3f42009-10-27 22:16:29 +00003244FPExtInst *FPExtInst::clone_impl() const {
3245 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003246}
3247
Devang Patel11cf3f42009-10-27 22:16:29 +00003248UIToFPInst *UIToFPInst::clone_impl() const {
3249 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003250}
3251
Devang Patel11cf3f42009-10-27 22:16:29 +00003252SIToFPInst *SIToFPInst::clone_impl() const {
3253 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003254}
3255
Devang Patel11cf3f42009-10-27 22:16:29 +00003256FPToUIInst *FPToUIInst::clone_impl() const {
3257 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003258}
3259
Devang Patel11cf3f42009-10-27 22:16:29 +00003260FPToSIInst *FPToSIInst::clone_impl() const {
3261 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003262}
3263
Devang Patel11cf3f42009-10-27 22:16:29 +00003264PtrToIntInst *PtrToIntInst::clone_impl() const {
3265 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003266}
3267
Devang Patel11cf3f42009-10-27 22:16:29 +00003268IntToPtrInst *IntToPtrInst::clone_impl() const {
3269 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003270}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003271
Devang Patel11cf3f42009-10-27 22:16:29 +00003272BitCastInst *BitCastInst::clone_impl() const {
3273 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003274}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003275
Devang Patel11cf3f42009-10-27 22:16:29 +00003276CallInst *CallInst::clone_impl() const {
3277 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003278}
3279
Devang Patel11cf3f42009-10-27 22:16:29 +00003280SelectInst *SelectInst::clone_impl() const {
3281 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003282}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003283
Devang Patel11cf3f42009-10-27 22:16:29 +00003284VAArgInst *VAArgInst::clone_impl() const {
3285 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003286}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003287
Devang Patel11cf3f42009-10-27 22:16:29 +00003288ExtractElementInst *ExtractElementInst::clone_impl() const {
3289 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003290}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003291
Devang Patel11cf3f42009-10-27 22:16:29 +00003292InsertElementInst *InsertElementInst::clone_impl() const {
3293 return InsertElementInst::Create(getOperand(0),
3294 getOperand(1),
3295 getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003296}
3297
Devang Patel11cf3f42009-10-27 22:16:29 +00003298ShuffleVectorInst *ShuffleVectorInst::clone_impl() const {
3299 return new ShuffleVectorInst(getOperand(0),
3300 getOperand(1),
3301 getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003302}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003303
Devang Patel11cf3f42009-10-27 22:16:29 +00003304PHINode *PHINode::clone_impl() const {
3305 return new PHINode(*this);
3306}
3307
3308ReturnInst *ReturnInst::clone_impl() const {
3309 return new(getNumOperands()) ReturnInst(*this);
3310}
3311
3312BranchInst *BranchInst::clone_impl() const {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00003313 unsigned Ops(getNumOperands());
Devang Patel11cf3f42009-10-27 22:16:29 +00003314 return new(Ops, Ops == 1) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003315}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003316
Devang Patel11cf3f42009-10-27 22:16:29 +00003317SwitchInst *SwitchInst::clone_impl() const {
3318 return new SwitchInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003319}
3320
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003321IndirectBrInst *IndirectBrInst::clone_impl() const {
3322 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003323}
3324
3325
Devang Patel11cf3f42009-10-27 22:16:29 +00003326InvokeInst *InvokeInst::clone_impl() const {
3327 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003328}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003329
Devang Patel11cf3f42009-10-27 22:16:29 +00003330UnwindInst *UnwindInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003331 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003332 return new UnwindInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003333}
3334
Devang Patel11cf3f42009-10-27 22:16:29 +00003335UnreachableInst *UnreachableInst::clone_impl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003336 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003337 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003338}