blob: d8bcb31a4766f64616aad56b9bbcf8a9e05f5731 [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
15#include "llvm/BasicBlock.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Function.h"
19#include "llvm/Instructions.h"
Reid Spencerce38beb2007-04-09 18:00:57 +000020#include "llvm/ParameterAttributes.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000021#include "llvm/Support/CallSite.h"
Reid Spencer0286bc12007-02-28 22:00:54 +000022#include "llvm/Support/ConstantRange.h"
Christopher Lamb84485702007-04-22 19:24:39 +000023#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000024using namespace llvm;
25
Chris Lattner3e13b8c2008-01-02 23:42:30 +000026//===----------------------------------------------------------------------===//
27// CallSite Class
28//===----------------------------------------------------------------------===//
29
Chris Lattnerf7b6d312005-05-06 20:26:43 +000030unsigned CallSite::getCallingConv() const {
31 if (CallInst *CI = dyn_cast<CallInst>(I))
32 return CI->getCallingConv();
33 else
34 return cast<InvokeInst>(I)->getCallingConv();
35}
36void CallSite::setCallingConv(unsigned CC) {
37 if (CallInst *CI = dyn_cast<CallInst>(I))
38 CI->setCallingConv(CC);
39 else
40 cast<InvokeInst>(I)->setCallingConv(CC);
41}
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000042const ParamAttrsList* CallSite::getParamAttrs() const {
43 if (CallInst *CI = dyn_cast<CallInst>(I))
44 return CI->getParamAttrs();
45 else
46 return cast<InvokeInst>(I)->getParamAttrs();
47}
48void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
49 if (CallInst *CI = dyn_cast<CallInst>(I))
50 CI->setParamAttrs(PAL);
51 else
52 cast<InvokeInst>(I)->setParamAttrs(PAL);
53}
Chris Lattner3e13b8c2008-01-02 23:42:30 +000054bool CallSite::paramHasAttr(uint16_t i, unsigned attr) const {
Duncan Sands5208d1a2007-11-28 17:07:01 +000055 if (CallInst *CI = dyn_cast<CallInst>(I))
Chris Lattner3e13b8c2008-01-02 23:42:30 +000056 return CI->paramHasAttr(i, (ParameterAttributes)attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000057 else
Chris Lattner3e13b8c2008-01-02 23:42:30 +000058 return cast<InvokeInst>(I)->paramHasAttr(i, (ParameterAttributes)attr);
Duncan Sands5208d1a2007-11-28 17:07:01 +000059}
Duncan Sands38ef3a82007-12-03 20:06:50 +000060bool CallSite::doesNotAccessMemory() const {
61 if (CallInst *CI = dyn_cast<CallInst>(I))
62 return CI->doesNotAccessMemory();
63 else
64 return cast<InvokeInst>(I)->doesNotAccessMemory();
65}
66bool CallSite::onlyReadsMemory() const {
67 if (CallInst *CI = dyn_cast<CallInst>(I))
68 return CI->onlyReadsMemory();
69 else
70 return cast<InvokeInst>(I)->onlyReadsMemory();
71}
Duncan Sands3353ed02007-12-18 09:59:50 +000072bool CallSite::doesNotThrow() const {
Duncan Sands8e4847e2007-12-16 15:51:49 +000073 if (CallInst *CI = dyn_cast<CallInst>(I))
Duncan Sands3353ed02007-12-18 09:59:50 +000074 return CI->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000075 else
Duncan Sands3353ed02007-12-18 09:59:50 +000076 return cast<InvokeInst>(I)->doesNotThrow();
Duncan Sands8e4847e2007-12-16 15:51:49 +000077}
Duncan Sandsaa31b922007-12-19 21:13:37 +000078void CallSite::setDoesNotThrow(bool doesNotThrow) {
79 if (CallInst *CI = dyn_cast<CallInst>(I))
80 CI->setDoesNotThrow(doesNotThrow);
81 else
82 cast<InvokeInst>(I)->setDoesNotThrow(doesNotThrow);
83}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000084
Gordon Henriksen14a55692007-12-10 02:14:30 +000085//===----------------------------------------------------------------------===//
86// TerminatorInst Class
87//===----------------------------------------------------------------------===//
88
89// Out of line virtual method, so the vtable, etc has a home.
90TerminatorInst::~TerminatorInst() {
91}
92
93// Out of line virtual method, so the vtable, etc has a home.
94UnaryInstruction::~UnaryInstruction() {
95}
96
97
Chris Lattnerafdb3de2005-01-29 00:35:16 +000098//===----------------------------------------------------------------------===//
99// PHINode Class
100//===----------------------------------------------------------------------===//
101
102PHINode::PHINode(const PHINode &PN)
103 : Instruction(PN.getType(), Instruction::PHI,
104 new Use[PN.getNumOperands()], PN.getNumOperands()),
105 ReservedSpace(PN.getNumOperands()) {
106 Use *OL = OperandList;
107 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
108 OL[i].init(PN.getOperand(i), this);
109 OL[i+1].init(PN.getOperand(i+1), this);
110 }
111}
112
Gordon Henriksen14a55692007-12-10 02:14:30 +0000113PHINode::~PHINode() {
114 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000115}
116
117// removeIncomingValue - Remove an incoming value. This is useful if a
118// predecessor basic block is deleted.
119Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
120 unsigned NumOps = getNumOperands();
121 Use *OL = OperandList;
122 assert(Idx*2 < NumOps && "BB not in PHI node!");
123 Value *Removed = OL[Idx*2];
124
125 // Move everything after this operand down.
126 //
127 // FIXME: we could just swap with the end of the list, then erase. However,
128 // client might not expect this to happen. The code as it is thrashes the
129 // use/def lists, which is kinda lame.
130 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
131 OL[i-2] = OL[i];
132 OL[i-2+1] = OL[i+1];
133 }
134
135 // Nuke the last value.
136 OL[NumOps-2].set(0);
137 OL[NumOps-2+1].set(0);
138 NumOperands = NumOps-2;
139
140 // If the PHI node is dead, because it has zero entries, nuke it now.
141 if (NumOps == 2 && DeletePHIIfEmpty) {
142 // If anyone is using this PHI, make them use a dummy value instead...
143 replaceAllUsesWith(UndefValue::get(getType()));
144 eraseFromParent();
145 }
146 return Removed;
147}
148
149/// resizeOperands - resize operands - This adjusts the length of the operands
150/// list according to the following behavior:
151/// 1. If NumOps == 0, grow the operand list in response to a push_back style
152/// of operation. This grows the number of ops by 1.5 times.
153/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
154/// 3. If NumOps == NumOperands, trim the reserved space.
155///
156void PHINode::resizeOperands(unsigned NumOps) {
157 if (NumOps == 0) {
158 NumOps = (getNumOperands())*3/2;
159 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
160 } else if (NumOps*2 > NumOperands) {
161 // No resize needed.
162 if (ReservedSpace >= NumOps) return;
163 } else if (NumOps == NumOperands) {
164 if (ReservedSpace == NumOps) return;
165 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000166 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000167 }
168
169 ReservedSpace = NumOps;
170 Use *NewOps = new Use[NumOps];
171 Use *OldOps = OperandList;
172 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
173 NewOps[i].init(OldOps[i], this);
174 OldOps[i].set(0);
175 }
176 delete [] OldOps;
177 OperandList = NewOps;
178}
179
Nate Begemanb3923212005-08-04 23:24:19 +0000180/// hasConstantValue - If the specified PHI node always merges together the same
181/// value, return the value, otherwise return null.
182///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000183Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000184 // If the PHI node only has one incoming value, eliminate the PHI node...
185 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000186 if (getIncomingValue(0) != this) // not X = phi X
187 return getIncomingValue(0);
188 else
189 return UndefValue::get(getType()); // Self cycle is dead.
190
Nate Begemanb3923212005-08-04 23:24:19 +0000191 // Otherwise if all of the incoming values are the same for the PHI, replace
192 // the PHI node with the incoming value.
193 //
194 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000195 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000196 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000197 if (isa<UndefValue>(getIncomingValue(i)))
198 HasUndefInput = true;
199 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000200 if (InVal && getIncomingValue(i) != InVal)
201 return 0; // Not the same, bail out.
202 else
203 InVal = getIncomingValue(i);
204
205 // The only case that could cause InVal to be null is if we have a PHI node
206 // that only has entries for itself. In this case, there is no entry into the
207 // loop, so kill the PHI.
208 //
209 if (InVal == 0) InVal = UndefValue::get(getType());
210
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000211 // If we have a PHI node like phi(X, undef, X), where X is defined by some
212 // instruction, we cannot always return X as the result of the PHI node. Only
213 // do this if X is not an instruction (thus it must dominate the PHI block),
214 // or if the client is prepared to deal with this possibility.
215 if (HasUndefInput && !AllowNonDominatingInstruction)
216 if (Instruction *IV = dyn_cast<Instruction>(InVal))
217 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000218 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000219 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000220 return 0; // Cannot guarantee that InVal dominates this PHINode.
221
Nate Begemanb3923212005-08-04 23:24:19 +0000222 // All of the incoming values are the same, return the value now.
223 return InVal;
224}
225
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000226
227//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000228// CallInst Implementation
229//===----------------------------------------------------------------------===//
230
Gordon Henriksen14a55692007-12-10 02:14:30 +0000231CallInst::~CallInst() {
232 delete [] OperandList;
233 if (ParamAttrs)
234 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000235}
236
Chris Lattner054ba2c2007-02-13 00:58:44 +0000237void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000238 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000239 NumOperands = NumParams+1;
240 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000241 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000242
Misha Brukmanb1c93172005-04-21 23:48:37 +0000243 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000244 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000245 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246
Chris Lattner054ba2c2007-02-13 00:58:44 +0000247 assert((NumParams == FTy->getNumParams() ||
248 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000249 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000250 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000251 assert((i >= FTy->getNumParams() ||
252 FTy->getParamType(i) == Params[i]->getType()) &&
253 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000254 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000255 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000256}
257
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000258void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000259 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000260 NumOperands = 3;
261 Use *OL = OperandList = new Use[3];
262 OL[0].init(Func, this);
263 OL[1].init(Actual1, this);
264 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000265
266 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000267 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000268 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000270 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000271 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000272 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000273 assert((0 >= FTy->getNumParams() ||
274 FTy->getParamType(0) == Actual1->getType()) &&
275 "Calling a function with a bad signature!");
276 assert((1 >= FTy->getNumParams() ||
277 FTy->getParamType(1) == Actual2->getType()) &&
278 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279}
280
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000281void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000282 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000283 NumOperands = 2;
284 Use *OL = OperandList = new Use[2];
285 OL[0].init(Func, this);
286 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000287
288 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000289 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000290 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000291
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000292 assert((FTy->getNumParams() == 1 ||
293 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000294 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000295 assert((0 == FTy->getNumParams() ||
296 FTy->getParamType(0) == Actual->getType()) &&
297 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000298}
299
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000300void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000301 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000302 NumOperands = 1;
303 Use *OL = OperandList = new Use[1];
304 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000305
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000306 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000308 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000309
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000310 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000311}
312
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000313CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000314 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000315 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
316 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000317 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000318 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000319 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000320}
321
322CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
323 BasicBlock *InsertAtEnd)
324 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
325 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000326 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000330CallInst::CallInst(Value *Func, const std::string &Name,
331 Instruction *InsertBefore)
332 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
333 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000334 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000335 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000336 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000337}
338
339CallInst::CallInst(Value *Func, const std::string &Name,
340 BasicBlock *InsertAtEnd)
341 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
342 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000343 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000344 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000345 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000346}
347
Misha Brukmanb1c93172005-04-21 23:48:37 +0000348CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000349 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000350 CI.getNumOperands()),
351 ParamAttrs(0) {
352 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000353 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000354 Use *OL = OperandList;
355 Use *InOL = CI.OperandList;
356 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
357 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000358}
359
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000360void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
361 if (ParamAttrs == newAttrs)
362 return;
363
Reid Spencerc6a83842007-04-22 17:28:03 +0000364 if (ParamAttrs)
365 ParamAttrs->dropRef();
366
367 if (newAttrs)
368 newAttrs->addRef();
369
370 ParamAttrs = newAttrs;
371}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000372
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000373bool CallInst::paramHasAttr(uint16_t i, unsigned attr) const {
374 if (ParamAttrs && ParamAttrs->paramHasAttr(i, (ParameterAttributes)attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000375 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000376 if (const Function *F = getCalledFunction())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000377 return F->paramHasAttr(i, (ParameterAttributes)attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000378 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000379}
380
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000381/// @brief Determine if the call does not access memory.
382bool CallInst::doesNotAccessMemory() const {
383 return paramHasAttr(0, ParamAttr::ReadNone);
384}
385
386/// @brief Determine if the call does not access or only reads memory.
387bool CallInst::onlyReadsMemory() const {
388 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
389}
390
391/// @brief Determine if the call cannot return.
392bool CallInst::doesNotReturn() const {
393 return paramHasAttr(0, ParamAttr::NoReturn);
394}
395
396/// @brief Determine if the call cannot unwind.
397bool CallInst::doesNotThrow() const {
398 return paramHasAttr(0, ParamAttr::NoUnwind);
399}
400
401/// @brief Determine if the call returns a structure.
402bool CallInst::isStructReturn() const {
403 // Be friendly and also check the callee.
404 return paramHasAttr(1, ParamAttr::StructRet);
405}
406
Duncan Sandsaa31b922007-12-19 21:13:37 +0000407void CallInst::setDoesNotThrow(bool doesNotThrow) {
408 const ParamAttrsList *PAL = getParamAttrs();
409 if (doesNotThrow)
410 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
411 else
412 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
413 setParamAttrs(PAL);
414}
415
Duncan Sands5208d1a2007-11-28 17:07:01 +0000416
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000417//===----------------------------------------------------------------------===//
418// InvokeInst Implementation
419//===----------------------------------------------------------------------===//
420
Gordon Henriksen14a55692007-12-10 02:14:30 +0000421InvokeInst::~InvokeInst() {
422 delete [] OperandList;
423 if (ParamAttrs)
424 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000425}
426
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000427void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000428 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000429 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000430 NumOperands = 3+NumArgs;
431 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000432 OL[0].init(Fn, this);
433 OL[1].init(IfNormal, this);
434 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000435 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000436 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000437 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000438
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000439 assert((NumArgs == FTy->getNumParams()) ||
440 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000441 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000442
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000443 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000444 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000445 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000446 "Invoking a function with a bad signature!");
447
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000448 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000449 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000450}
451
Misha Brukmanb1c93172005-04-21 23:48:37 +0000452InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000453 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000454 new Use[II.getNumOperands()], II.getNumOperands()),
455 ParamAttrs(0) {
456 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000457 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000458 Use *OL = OperandList, *InOL = II.OperandList;
459 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
460 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000461}
462
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000463BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
464 return getSuccessor(idx);
465}
466unsigned InvokeInst::getNumSuccessorsV() const {
467 return getNumSuccessors();
468}
469void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
470 return setSuccessor(idx, B);
471}
472
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000473void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
474 if (ParamAttrs == newAttrs)
475 return;
476
Reid Spencerc6a83842007-04-22 17:28:03 +0000477 if (ParamAttrs)
478 ParamAttrs->dropRef();
479
480 if (newAttrs)
481 newAttrs->addRef();
482
483 ParamAttrs = newAttrs;
484}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000485
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000486bool InvokeInst::paramHasAttr(uint16_t i, unsigned attr) const {
487 if (ParamAttrs && ParamAttrs->paramHasAttr(i, (ParameterAttributes)attr))
Duncan Sands5208d1a2007-11-28 17:07:01 +0000488 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000489 if (const Function *F = getCalledFunction())
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000490 return F->paramHasAttr(i, (ParameterAttributes)attr);
Duncan Sands8dfcd592007-11-29 08:30:15 +0000491 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000492}
493
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000494
495/// @brief Determine if the call does not access memory.
496bool InvokeInst::doesNotAccessMemory() const {
497 return paramHasAttr(0, ParamAttr::ReadNone);
498}
499
500/// @brief Determine if the call does not access or only reads memory.
501bool InvokeInst::onlyReadsMemory() const {
502 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
503}
504
505/// @brief Determine if the call cannot return.
506bool InvokeInst::doesNotReturn() const {
507 return paramHasAttr(0, ParamAttr::NoReturn);
508}
509
510/// @brief Determine if the call cannot unwind.
511bool InvokeInst::doesNotThrow() const {
512 return paramHasAttr(0, ParamAttr::NoUnwind);
513}
514
Duncan Sandsaa31b922007-12-19 21:13:37 +0000515void InvokeInst::setDoesNotThrow(bool doesNotThrow) {
516 const ParamAttrsList *PAL = getParamAttrs();
517 if (doesNotThrow)
518 PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
519 else
520 PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
521 setParamAttrs(PAL);
522}
523
Chris Lattner3e13b8c2008-01-02 23:42:30 +0000524/// @brief Determine if the call returns a structure.
525bool InvokeInst::isStructReturn() const {
526 // Be friendly and also check the callee.
527 return paramHasAttr(1, ParamAttr::StructRet);
528}
529
Duncan Sands5208d1a2007-11-28 17:07:01 +0000530
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000531//===----------------------------------------------------------------------===//
532// ReturnInst Implementation
533//===----------------------------------------------------------------------===//
534
Chris Lattner2195fc42007-02-24 00:55:48 +0000535ReturnInst::ReturnInst(const ReturnInst &RI)
536 : TerminatorInst(Type::VoidTy, Instruction::Ret,
537 &RetVal, RI.getNumOperands()) {
538 if (RI.getNumOperands())
539 RetVal.init(RI.RetVal, this);
540}
541
542ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
543 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
544 init(retVal);
545}
546ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
547 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
548 init(retVal);
549}
550ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
551 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
552}
553
554
555
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000556void ReturnInst::init(Value *retVal) {
557 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000558 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000559 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000560 NumOperands = 1;
561 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000562 }
563}
564
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000565unsigned ReturnInst::getNumSuccessorsV() const {
566 return getNumSuccessors();
567}
568
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000569// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
570// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000571void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000572 assert(0 && "ReturnInst has no successors!");
573}
574
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000575BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
576 assert(0 && "ReturnInst has no successors!");
577 abort();
578 return 0;
579}
580
581
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000582//===----------------------------------------------------------------------===//
583// UnwindInst Implementation
584//===----------------------------------------------------------------------===//
585
Chris Lattner2195fc42007-02-24 00:55:48 +0000586UnwindInst::UnwindInst(Instruction *InsertBefore)
587 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
588}
589UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
590 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
591}
592
593
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000594unsigned UnwindInst::getNumSuccessorsV() const {
595 return getNumSuccessors();
596}
597
598void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000599 assert(0 && "UnwindInst has no successors!");
600}
601
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
603 assert(0 && "UnwindInst has no successors!");
604 abort();
605 return 0;
606}
607
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000608//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000609// UnreachableInst Implementation
610//===----------------------------------------------------------------------===//
611
Chris Lattner2195fc42007-02-24 00:55:48 +0000612UnreachableInst::UnreachableInst(Instruction *InsertBefore)
613 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
614}
615UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
616 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
617}
618
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000619unsigned UnreachableInst::getNumSuccessorsV() const {
620 return getNumSuccessors();
621}
622
623void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
624 assert(0 && "UnwindInst has no successors!");
625}
626
627BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
628 assert(0 && "UnwindInst has no successors!");
629 abort();
630 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000631}
632
633//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000634// BranchInst Implementation
635//===----------------------------------------------------------------------===//
636
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000637void BranchInst::AssertOK() {
638 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000639 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000640 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000641}
642
Chris Lattner2195fc42007-02-24 00:55:48 +0000643BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
644 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
645 assert(IfTrue != 0 && "Branch destination may not be null!");
646 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
647}
648BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
649 Instruction *InsertBefore)
650: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
651 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
652 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
653 Ops[2].init(Cond, this);
654#ifndef NDEBUG
655 AssertOK();
656#endif
657}
658
659BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
660 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
661 assert(IfTrue != 0 && "Branch destination may not be null!");
662 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
663}
664
665BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
666 BasicBlock *InsertAtEnd)
667 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
668 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
669 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
670 Ops[2].init(Cond, this);
671#ifndef NDEBUG
672 AssertOK();
673#endif
674}
675
676
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000677BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000678 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000679 OperandList[0].init(BI.getOperand(0), this);
680 if (BI.getNumOperands() != 1) {
681 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
682 OperandList[1].init(BI.getOperand(1), this);
683 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684 }
685}
686
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000687BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
688 return getSuccessor(idx);
689}
690unsigned BranchInst::getNumSuccessorsV() const {
691 return getNumSuccessors();
692}
693void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
694 setSuccessor(idx, B);
695}
696
697
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698//===----------------------------------------------------------------------===//
699// AllocationInst Implementation
700//===----------------------------------------------------------------------===//
701
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000702static Value *getAISize(Value *Amt) {
703 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000704 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000705 else {
706 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000707 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000708 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000709 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000710 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000711 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000712}
713
Misha Brukmanb1c93172005-04-21 23:48:37 +0000714AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000715 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000716 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000717 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000718 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000719 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000720 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000721 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000722}
723
Misha Brukmanb1c93172005-04-21 23:48:37 +0000724AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000725 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000726 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000727 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000728 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000729 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000731 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000732}
733
Gordon Henriksen14a55692007-12-10 02:14:30 +0000734// Out of line virtual method, so the vtable, etc has a home.
735AllocationInst::~AllocationInst() {
736}
737
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000738bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000739 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
740 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000741 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000742}
743
744const Type *AllocationInst::getAllocatedType() const {
745 return getType()->getElementType();
746}
747
748AllocaInst::AllocaInst(const AllocaInst &AI)
749 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000750 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000751}
752
753MallocInst::MallocInst(const MallocInst &MI)
754 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000755 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000756}
757
758//===----------------------------------------------------------------------===//
759// FreeInst Implementation
760//===----------------------------------------------------------------------===//
761
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000762void FreeInst::AssertOK() {
763 assert(isa<PointerType>(getOperand(0)->getType()) &&
764 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000765}
766
767FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000768 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000769 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000770}
771
772FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000773 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000774 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000775}
776
777
778//===----------------------------------------------------------------------===//
779// LoadInst Implementation
780//===----------------------------------------------------------------------===//
781
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000782void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000783 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000784 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000785}
786
787LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000788 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000789 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000790 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000791 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000792 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000793 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000794}
795
796LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000797 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000798 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000799 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000800 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000801 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000802 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000803}
804
805LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
806 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000807 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000808 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000809 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000810 setAlignment(0);
811 AssertOK();
812 setName(Name);
813}
814
815LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
816 unsigned Align, Instruction *InsertBef)
817 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
818 Load, Ptr, InsertBef) {
819 setVolatile(isVolatile);
820 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000821 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000822 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000823}
824
Dan Gohman68659282007-07-18 20:51:11 +0000825LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
826 unsigned Align, BasicBlock *InsertAE)
827 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
828 Load, Ptr, InsertAE) {
829 setVolatile(isVolatile);
830 setAlignment(Align);
831 AssertOK();
832 setName(Name);
833}
834
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000835LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
836 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000837 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000838 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000839 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000840 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000841 AssertOK();
842 setName(Name);
843}
844
845
846
847LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000848 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
849 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000850 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000851 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000852 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000853 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000854}
855
856LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000857 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
858 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000859 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000860 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000861 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000862 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000863}
864
865LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
866 Instruction *InsertBef)
867: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000868 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000869 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000870 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000871 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000872 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000873}
874
875LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
876 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000877 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
878 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000879 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000880 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000881 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000882 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000883}
884
Christopher Lamb84485702007-04-22 19:24:39 +0000885void LoadInst::setAlignment(unsigned Align) {
886 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
887 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
888}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000889
890//===----------------------------------------------------------------------===//
891// StoreInst Implementation
892//===----------------------------------------------------------------------===//
893
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000894void StoreInst::AssertOK() {
895 assert(isa<PointerType>(getOperand(1)->getType()) &&
896 "Ptr must have pointer type!");
897 assert(getOperand(0)->getType() ==
898 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000899 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000900}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000901
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000902
903StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000904 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000905 Ops[0].init(val, this);
906 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000907 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000908 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000909 AssertOK();
910}
911
912StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000913 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000914 Ops[0].init(val, this);
915 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000916 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000917 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000918 AssertOK();
919}
920
Misha Brukmanb1c93172005-04-21 23:48:37 +0000921StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000922 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000923 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000924 Ops[0].init(val, this);
925 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000926 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000927 setAlignment(0);
928 AssertOK();
929}
930
931StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
932 unsigned Align, Instruction *InsertBefore)
933 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
934 Ops[0].init(val, this);
935 Ops[1].init(addr, this);
936 setVolatile(isVolatile);
937 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000938 AssertOK();
939}
940
Misha Brukmanb1c93172005-04-21 23:48:37 +0000941StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000942 unsigned Align, BasicBlock *InsertAtEnd)
943 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
944 Ops[0].init(val, this);
945 Ops[1].init(addr, this);
946 setVolatile(isVolatile);
947 setAlignment(Align);
948 AssertOK();
949}
950
951StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000952 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000953 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000954 Ops[0].init(val, this);
955 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000956 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000957 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000958 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000959}
960
Christopher Lamb84485702007-04-22 19:24:39 +0000961void StoreInst::setAlignment(unsigned Align) {
962 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
963 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
964}
965
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000966//===----------------------------------------------------------------------===//
967// GetElementPtrInst Implementation
968//===----------------------------------------------------------------------===//
969
Christopher Lambedf07882007-12-17 01:12:55 +0000970static unsigned retrieveAddrSpace(const Value *Val) {
971 return cast<PointerType>(Val->getType())->getAddressSpace();
972}
973
Chris Lattner79807c3d2007-01-31 19:47:18 +0000974void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
975 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000976 Use *OL = OperandList = new Use[NumOperands];
977 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000978
Chris Lattner79807c3d2007-01-31 19:47:18 +0000979 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000980 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000981}
982
Chris Lattner82981202005-05-03 05:43:30 +0000983void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
984 NumOperands = 2;
985 Use *OL = OperandList = new Use[2];
986 OL[0].init(Ptr, this);
987 OL[1].init(Idx, this);
988}
989
Chris Lattner82981202005-05-03 05:43:30 +0000990GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
991 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000992 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +0000993 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +0000994 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000995 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000996 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000997}
998
999GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
1000 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +00001001 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +00001002 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +00001003 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +00001004 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +00001005 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001006}
1007
Gordon Henriksen14a55692007-12-10 02:14:30 +00001008GetElementPtrInst::~GetElementPtrInst() {
1009 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001010}
1011
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012// getIndexedType - Returns the type of the element that would be loaded with
1013// a load instruction with the specified parameters.
1014//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001015// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001016// pointer type.
1017//
Misha Brukmanb1c93172005-04-21 23:48:37 +00001018const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +00001019 Value* const *Idxs,
1020 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001021 bool AllowCompositeLeaf) {
1022 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
1023
1024 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +00001025 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001026 if (AllowCompositeLeaf ||
1027 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
1028 return cast<PointerType>(Ptr)->getElementType();
1029 else
1030 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001031
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001032 unsigned CurIdx = 0;
1033 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +00001034 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001035 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
1036 return 0; // Can't load a whole structure or array!?!?
1037 }
1038
Chris Lattner302116a2007-01-31 04:40:28 +00001039 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001040 if (isa<PointerType>(CT) && CurIdx != 1)
1041 return 0; // Can only index into pointer types at the first index!
1042 if (!CT->indexValid(Index)) return 0;
1043 Ptr = CT->getTypeAtIndex(Index);
1044
1045 // If the new type forwards to another type, then it is in the middle
1046 // of being refined to another type (and hence, may have dropped all
1047 // references to what it was using before). So, use the new forwarded
1048 // type.
1049 if (const Type * Ty = Ptr->getForwardedType()) {
1050 Ptr = Ty;
1051 }
1052 }
Chris Lattner302116a2007-01-31 04:40:28 +00001053 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001054}
1055
Chris Lattner82981202005-05-03 05:43:30 +00001056const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1057 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1058 if (!PTy) return 0; // Type isn't a pointer type!
1059
1060 // Check the pointer index.
1061 if (!PTy->indexValid(Idx)) return 0;
1062
Chris Lattnerc2233332005-05-03 16:44:45 +00001063 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001064}
1065
Chris Lattner45f15572007-04-14 00:12:57 +00001066
1067/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1068/// zeros. If so, the result pointer and the first operand have the same
1069/// value, just potentially different types.
1070bool GetElementPtrInst::hasAllZeroIndices() const {
1071 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1072 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1073 if (!CI->isZero()) return false;
1074 } else {
1075 return false;
1076 }
1077 }
1078 return true;
1079}
1080
Chris Lattner27058292007-04-27 20:35:56 +00001081/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1082/// constant integers. If so, the result pointer and the first operand have
1083/// a constant offset between them.
1084bool GetElementPtrInst::hasAllConstantIndices() const {
1085 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1086 if (!isa<ConstantInt>(getOperand(i)))
1087 return false;
1088 }
1089 return true;
1090}
1091
Chris Lattner45f15572007-04-14 00:12:57 +00001092
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001093//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001094// ExtractElementInst Implementation
1095//===----------------------------------------------------------------------===//
1096
1097ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001098 const std::string &Name,
1099 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001100 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001101 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001102 assert(isValidOperands(Val, Index) &&
1103 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001104 Ops[0].init(Val, this);
1105 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001106 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001107}
1108
Chris Lattner65511ff2006-10-05 06:24:58 +00001109ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1110 const std::string &Name,
1111 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001112 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001113 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001114 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001115 assert(isValidOperands(Val, Index) &&
1116 "Invalid extractelement instruction operands!");
1117 Ops[0].init(Val, this);
1118 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001119 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001120}
1121
1122
Robert Bocchino23004482006-01-10 19:05:34 +00001123ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001124 const std::string &Name,
1125 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001126 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001127 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001128 assert(isValidOperands(Val, Index) &&
1129 "Invalid extractelement instruction operands!");
1130
Robert Bocchino23004482006-01-10 19:05:34 +00001131 Ops[0].init(Val, this);
1132 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001133 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001134}
1135
Chris Lattner65511ff2006-10-05 06:24:58 +00001136ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1137 const std::string &Name,
1138 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001139 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001140 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001141 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001142 assert(isValidOperands(Val, Index) &&
1143 "Invalid extractelement instruction operands!");
1144
1145 Ops[0].init(Val, this);
1146 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001147 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001148}
1149
1150
Chris Lattner54865b32006-04-08 04:05:48 +00001151bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001152 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001153 return false;
1154 return true;
1155}
1156
1157
Robert Bocchino23004482006-01-10 19:05:34 +00001158//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001159// InsertElementInst Implementation
1160//===----------------------------------------------------------------------===//
1161
Chris Lattner0875d942006-04-14 22:20:32 +00001162InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1163 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1164 Ops[0].init(IE.Ops[0], this);
1165 Ops[1].init(IE.Ops[1], this);
1166 Ops[2].init(IE.Ops[2], this);
1167}
Chris Lattner54865b32006-04-08 04:05:48 +00001168InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001169 const std::string &Name,
1170 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001171 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001172 assert(isValidOperands(Vec, Elt, Index) &&
1173 "Invalid insertelement instruction operands!");
1174 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001175 Ops[1].init(Elt, this);
1176 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001177 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001178}
1179
Chris Lattner65511ff2006-10-05 06:24:58 +00001180InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1181 const std::string &Name,
1182 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001183 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001184 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001185 assert(isValidOperands(Vec, Elt, Index) &&
1186 "Invalid insertelement instruction operands!");
1187 Ops[0].init(Vec, this);
1188 Ops[1].init(Elt, this);
1189 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001190 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001191}
1192
1193
Chris Lattner54865b32006-04-08 04:05:48 +00001194InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001195 const std::string &Name,
1196 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001197 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001198 assert(isValidOperands(Vec, Elt, Index) &&
1199 "Invalid insertelement instruction operands!");
1200
1201 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001202 Ops[1].init(Elt, this);
1203 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001204 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001205}
1206
Chris Lattner65511ff2006-10-05 06:24:58 +00001207InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1208 const std::string &Name,
1209 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001210: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001211 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001212 assert(isValidOperands(Vec, Elt, Index) &&
1213 "Invalid insertelement instruction operands!");
1214
1215 Ops[0].init(Vec, this);
1216 Ops[1].init(Elt, this);
1217 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001218 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001219}
1220
Chris Lattner54865b32006-04-08 04:05:48 +00001221bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1222 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001223 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001224 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001225
Reid Spencerd84d35b2007-02-15 02:26:10 +00001226 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001227 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001228
Reid Spencer8d9336d2006-12-31 05:26:44 +00001229 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001230 return false; // Third operand of insertelement must be uint.
1231 return true;
1232}
1233
1234
Robert Bocchinoca27f032006-01-17 20:07:22 +00001235//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001236// ShuffleVectorInst Implementation
1237//===----------------------------------------------------------------------===//
1238
Chris Lattner0875d942006-04-14 22:20:32 +00001239ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1240 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1241 Ops[0].init(SV.Ops[0], this);
1242 Ops[1].init(SV.Ops[1], this);
1243 Ops[2].init(SV.Ops[2], this);
1244}
1245
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001246ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1247 const std::string &Name,
1248 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001249 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001250 assert(isValidOperands(V1, V2, Mask) &&
1251 "Invalid shuffle vector instruction operands!");
1252 Ops[0].init(V1, this);
1253 Ops[1].init(V2, this);
1254 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001255 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001256}
1257
1258ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1259 const std::string &Name,
1260 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001261 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001262 assert(isValidOperands(V1, V2, Mask) &&
1263 "Invalid shuffle vector instruction operands!");
1264
1265 Ops[0].init(V1, this);
1266 Ops[1].init(V2, this);
1267 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001268 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001269}
1270
1271bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1272 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001273 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001274 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001275 if (!isa<VectorType>(Mask->getType()) ||
1276 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1277 cast<VectorType>(Mask->getType())->getNumElements() !=
1278 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001279 return false;
1280 return true;
1281}
1282
1283
1284//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001285// BinaryOperator Class
1286//===----------------------------------------------------------------------===//
1287
Chris Lattner2195fc42007-02-24 00:55:48 +00001288BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1289 const Type *Ty, const std::string &Name,
1290 Instruction *InsertBefore)
1291 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1292 Ops[0].init(S1, this);
1293 Ops[1].init(S2, this);
1294 init(iType);
1295 setName(Name);
1296}
1297
1298BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1299 const Type *Ty, const std::string &Name,
1300 BasicBlock *InsertAtEnd)
1301 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1302 Ops[0].init(S1, this);
1303 Ops[1].init(S2, this);
1304 init(iType);
1305 setName(Name);
1306}
1307
1308
1309void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001310 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001311 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001312 assert(LHS->getType() == RHS->getType() &&
1313 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001314#ifndef NDEBUG
1315 switch (iType) {
1316 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001317 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001318 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001319 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001320 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001321 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001322 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001323 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001324 case UDiv:
1325 case SDiv:
1326 assert(getType() == LHS->getType() &&
1327 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001328 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1329 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001330 "Incorrect operand type (not integer) for S/UDIV");
1331 break;
1332 case FDiv:
1333 assert(getType() == LHS->getType() &&
1334 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001335 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1336 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001337 && "Incorrect operand type (not floating point) for FDIV");
1338 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001339 case URem:
1340 case SRem:
1341 assert(getType() == LHS->getType() &&
1342 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001343 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1344 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001345 "Incorrect operand type (not integer) for S/UREM");
1346 break;
1347 case FRem:
1348 assert(getType() == LHS->getType() &&
1349 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001350 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1351 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001352 && "Incorrect operand type (not floating point) for FREM");
1353 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001354 case Shl:
1355 case LShr:
1356 case AShr:
1357 assert(getType() == LHS->getType() &&
1358 "Shift operation should return same type as operands!");
1359 assert(getType()->isInteger() &&
1360 "Shift operation requires integer operands");
1361 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001362 case And: case Or:
1363 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001364 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001365 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001366 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001367 (isa<VectorType>(getType()) &&
1368 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001369 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001370 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001371 default:
1372 break;
1373 }
1374#endif
1375}
1376
1377BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001378 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001379 Instruction *InsertBefore) {
1380 assert(S1->getType() == S2->getType() &&
1381 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001382 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001383}
1384
1385BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001386 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001387 BasicBlock *InsertAtEnd) {
1388 BinaryOperator *Res = create(Op, S1, S2, Name);
1389 InsertAtEnd->getInstList().push_back(Res);
1390 return Res;
1391}
1392
1393BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1394 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001395 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1396 return new BinaryOperator(Instruction::Sub,
1397 zero, Op,
1398 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001399}
1400
1401BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1402 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001403 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1404 return new BinaryOperator(Instruction::Sub,
1405 zero, Op,
1406 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001407}
1408
1409BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1410 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001411 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001412 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001413 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001414 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001415 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001416 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001417 }
1418
1419 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001420 Op->getType(), Name, InsertBefore);
1421}
1422
1423BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1424 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001425 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001426 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001427 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001428 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001429 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001430 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001431 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001432 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001433 }
1434
1435 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001436 Op->getType(), Name, InsertAtEnd);
1437}
1438
1439
1440// isConstantAllOnes - Helper function for several functions below
1441static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001442 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1443 return CI->isAllOnesValue();
1444 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1445 return CV->isAllOnesValue();
1446 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001447}
1448
1449bool BinaryOperator::isNeg(const Value *V) {
1450 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1451 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001452 return Bop->getOperand(0) ==
1453 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001454 return false;
1455}
1456
1457bool BinaryOperator::isNot(const Value *V) {
1458 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1459 return (Bop->getOpcode() == Instruction::Xor &&
1460 (isConstantAllOnes(Bop->getOperand(1)) ||
1461 isConstantAllOnes(Bop->getOperand(0))));
1462 return false;
1463}
1464
Chris Lattner2c7d1772005-04-24 07:28:37 +00001465Value *BinaryOperator::getNegArgument(Value *BinOp) {
1466 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1467 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001468}
1469
Chris Lattner2c7d1772005-04-24 07:28:37 +00001470const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1471 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001472}
1473
Chris Lattner2c7d1772005-04-24 07:28:37 +00001474Value *BinaryOperator::getNotArgument(Value *BinOp) {
1475 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1476 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1477 Value *Op0 = BO->getOperand(0);
1478 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001479 if (isConstantAllOnes(Op0)) return Op1;
1480
1481 assert(isConstantAllOnes(Op1));
1482 return Op0;
1483}
1484
Chris Lattner2c7d1772005-04-24 07:28:37 +00001485const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1486 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001487}
1488
1489
1490// swapOperands - Exchange the two operands to this instruction. This
1491// instruction is safe to use on any binary instruction and does not
1492// modify the semantics of the instruction. If the instruction is
1493// order dependent (SetLT f.e.) the opcode is changed.
1494//
1495bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001496 if (!isCommutative())
1497 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001498 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001499 return false;
1500}
1501
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001502//===----------------------------------------------------------------------===//
1503// CastInst Class
1504//===----------------------------------------------------------------------===//
1505
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001506// Just determine if this cast only deals with integral->integral conversion.
1507bool CastInst::isIntegerCast() const {
1508 switch (getOpcode()) {
1509 default: return false;
1510 case Instruction::ZExt:
1511 case Instruction::SExt:
1512 case Instruction::Trunc:
1513 return true;
1514 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001515 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001516 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001517}
1518
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001519bool CastInst::isLosslessCast() const {
1520 // Only BitCast can be lossless, exit fast if we're not BitCast
1521 if (getOpcode() != Instruction::BitCast)
1522 return false;
1523
1524 // Identity cast is always lossless
1525 const Type* SrcTy = getOperand(0)->getType();
1526 const Type* DstTy = getType();
1527 if (SrcTy == DstTy)
1528 return true;
1529
Reid Spencer8d9336d2006-12-31 05:26:44 +00001530 // Pointer to pointer is always lossless.
1531 if (isa<PointerType>(SrcTy))
1532 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001533 return false; // Other types have no identity values
1534}
1535
1536/// This function determines if the CastInst does not require any bits to be
1537/// changed in order to effect the cast. Essentially, it identifies cases where
1538/// no code gen is necessary for the cast, hence the name no-op cast. For
1539/// example, the following are all no-op casts:
1540/// # bitcast uint %X, int
1541/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001542/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001543/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1544/// @brief Determine if a cast is a no-op.
1545bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1546 switch (getOpcode()) {
1547 default:
1548 assert(!"Invalid CastOp");
1549 case Instruction::Trunc:
1550 case Instruction::ZExt:
1551 case Instruction::SExt:
1552 case Instruction::FPTrunc:
1553 case Instruction::FPExt:
1554 case Instruction::UIToFP:
1555 case Instruction::SIToFP:
1556 case Instruction::FPToUI:
1557 case Instruction::FPToSI:
1558 return false; // These always modify bits
1559 case Instruction::BitCast:
1560 return true; // BitCast never modifies bits.
1561 case Instruction::PtrToInt:
1562 return IntPtrTy->getPrimitiveSizeInBits() ==
1563 getType()->getPrimitiveSizeInBits();
1564 case Instruction::IntToPtr:
1565 return IntPtrTy->getPrimitiveSizeInBits() ==
1566 getOperand(0)->getType()->getPrimitiveSizeInBits();
1567 }
1568}
1569
1570/// This function determines if a pair of casts can be eliminated and what
1571/// opcode should be used in the elimination. This assumes that there are two
1572/// instructions like this:
1573/// * %F = firstOpcode SrcTy %x to MidTy
1574/// * %S = secondOpcode MidTy %F to DstTy
1575/// The function returns a resultOpcode so these two casts can be replaced with:
1576/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1577/// If no such cast is permited, the function returns 0.
1578unsigned CastInst::isEliminableCastPair(
1579 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1580 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1581{
1582 // Define the 144 possibilities for these two cast instructions. The values
1583 // in this matrix determine what to do in a given situation and select the
1584 // case in the switch below. The rows correspond to firstOp, the columns
1585 // correspond to secondOp. In looking at the table below, keep in mind
1586 // the following cast properties:
1587 //
1588 // Size Compare Source Destination
1589 // Operator Src ? Size Type Sign Type Sign
1590 // -------- ------------ ------------------- ---------------------
1591 // TRUNC > Integer Any Integral Any
1592 // ZEXT < Integral Unsigned Integer Any
1593 // SEXT < Integral Signed Integer Any
1594 // FPTOUI n/a FloatPt n/a Integral Unsigned
1595 // FPTOSI n/a FloatPt n/a Integral Signed
1596 // UITOFP n/a Integral Unsigned FloatPt n/a
1597 // SITOFP n/a Integral Signed FloatPt n/a
1598 // FPTRUNC > FloatPt n/a FloatPt n/a
1599 // FPEXT < FloatPt n/a FloatPt n/a
1600 // PTRTOINT n/a Pointer n/a Integral Unsigned
1601 // INTTOPTR n/a Integral Unsigned Pointer n/a
1602 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001603 //
1604 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1605 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1606 // into "fptoui double to ulong", but this loses information about the range
1607 // of the produced value (we no longer know the top-part is all zeros).
1608 // Further this conversion is often much more expensive for typical hardware,
1609 // and causes issues when building libgcc. We disallow fptosi+sext for the
1610 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001611 const unsigned numCastOps =
1612 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1613 static const uint8_t CastResults[numCastOps][numCastOps] = {
1614 // T F F U S F F P I B -+
1615 // R Z S P P I I T P 2 N T |
1616 // U E E 2 2 2 2 R E I T C +- secondOp
1617 // N X X U S F F N X N 2 V |
1618 // C T T I I P P C T T P T -+
1619 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1620 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1621 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001622 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1623 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001624 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1625 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1626 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1627 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1628 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1629 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1630 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1631 };
1632
1633 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1634 [secondOp-Instruction::CastOpsBegin];
1635 switch (ElimCase) {
1636 case 0:
1637 // categorically disallowed
1638 return 0;
1639 case 1:
1640 // allowed, use first cast's opcode
1641 return firstOp;
1642 case 2:
1643 // allowed, use second cast's opcode
1644 return secondOp;
1645 case 3:
1646 // no-op cast in second op implies firstOp as long as the DestTy
1647 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001648 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001649 return firstOp;
1650 return 0;
1651 case 4:
1652 // no-op cast in second op implies firstOp as long as the DestTy
1653 // is floating point
1654 if (DstTy->isFloatingPoint())
1655 return firstOp;
1656 return 0;
1657 case 5:
1658 // no-op cast in first op implies secondOp as long as the SrcTy
1659 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001660 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001661 return secondOp;
1662 return 0;
1663 case 6:
1664 // no-op cast in first op implies secondOp as long as the SrcTy
1665 // is a floating point
1666 if (SrcTy->isFloatingPoint())
1667 return secondOp;
1668 return 0;
1669 case 7: {
1670 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1671 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1672 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1673 if (MidSize >= PtrSize)
1674 return Instruction::BitCast;
1675 return 0;
1676 }
1677 case 8: {
1678 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1679 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1680 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1681 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1682 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1683 if (SrcSize == DstSize)
1684 return Instruction::BitCast;
1685 else if (SrcSize < DstSize)
1686 return firstOp;
1687 return secondOp;
1688 }
1689 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1690 return Instruction::ZExt;
1691 case 10:
1692 // fpext followed by ftrunc is allowed if the bit size returned to is
1693 // the same as the original, in which case its just a bitcast
1694 if (SrcTy == DstTy)
1695 return Instruction::BitCast;
1696 return 0; // If the types are not the same we can't eliminate it.
1697 case 11:
1698 // bitcast followed by ptrtoint is allowed as long as the bitcast
1699 // is a pointer to pointer cast.
1700 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1701 return secondOp;
1702 return 0;
1703 case 12:
1704 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1705 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1706 return firstOp;
1707 return 0;
1708 case 13: {
1709 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1710 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1711 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1712 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1713 if (SrcSize <= PtrSize && SrcSize == DstSize)
1714 return Instruction::BitCast;
1715 return 0;
1716 }
1717 case 99:
1718 // cast combination can't happen (error in input). This is for all cases
1719 // where the MidTy is not the same for the two cast instructions.
1720 assert(!"Invalid Cast Combination");
1721 return 0;
1722 default:
1723 assert(!"Error in CastResults table!!!");
1724 return 0;
1725 }
1726 return 0;
1727}
1728
1729CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1730 const std::string &Name, Instruction *InsertBefore) {
1731 // Construct and return the appropriate CastInst subclass
1732 switch (op) {
1733 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1734 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1735 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1736 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1737 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1738 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1739 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1740 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1741 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1742 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1743 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1744 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1745 default:
1746 assert(!"Invalid opcode provided");
1747 }
1748 return 0;
1749}
1750
1751CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1752 const std::string &Name, BasicBlock *InsertAtEnd) {
1753 // Construct and return the appropriate CastInst subclass
1754 switch (op) {
1755 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1756 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1757 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1758 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1759 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1760 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1761 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1762 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1763 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1764 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1765 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1766 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1767 default:
1768 assert(!"Invalid opcode provided");
1769 }
1770 return 0;
1771}
1772
Reid Spencer5c140882006-12-04 20:17:56 +00001773CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1774 const std::string &Name,
1775 Instruction *InsertBefore) {
1776 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1777 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1778 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1779}
1780
1781CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1782 const std::string &Name,
1783 BasicBlock *InsertAtEnd) {
1784 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1785 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1786 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1787}
1788
1789CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1790 const std::string &Name,
1791 Instruction *InsertBefore) {
1792 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1793 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1794 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1795}
1796
1797CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1798 const std::string &Name,
1799 BasicBlock *InsertAtEnd) {
1800 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1801 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1802 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1803}
1804
1805CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1806 const std::string &Name,
1807 Instruction *InsertBefore) {
1808 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1809 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1810 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1811}
1812
1813CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1814 const std::string &Name,
1815 BasicBlock *InsertAtEnd) {
1816 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1817 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1818 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1819}
1820
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001821CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1822 const std::string &Name,
1823 BasicBlock *InsertAtEnd) {
1824 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001825 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001826 "Invalid cast");
1827
Chris Lattner03c49532007-01-15 02:27:26 +00001828 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001829 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1830 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1831}
1832
1833/// @brief Create a BitCast or a PtrToInt cast instruction
1834CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1835 const std::string &Name,
1836 Instruction *InsertBefore) {
1837 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001838 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001839 "Invalid cast");
1840
Chris Lattner03c49532007-01-15 02:27:26 +00001841 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001842 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1843 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1844}
1845
Reid Spencer7e933472006-12-12 00:49:44 +00001846CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1847 bool isSigned, const std::string &Name,
1848 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001849 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001850 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1851 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1852 Instruction::CastOps opcode =
1853 (SrcBits == DstBits ? Instruction::BitCast :
1854 (SrcBits > DstBits ? Instruction::Trunc :
1855 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1856 return create(opcode, C, Ty, Name, InsertBefore);
1857}
1858
1859CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1860 bool isSigned, const std::string &Name,
1861 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001862 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001863 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1864 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1865 Instruction::CastOps opcode =
1866 (SrcBits == DstBits ? Instruction::BitCast :
1867 (SrcBits > DstBits ? Instruction::Trunc :
1868 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1869 return create(opcode, C, Ty, Name, InsertAtEnd);
1870}
1871
1872CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1873 const std::string &Name,
1874 Instruction *InsertBefore) {
1875 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1876 "Invalid cast");
1877 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1878 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1879 Instruction::CastOps opcode =
1880 (SrcBits == DstBits ? Instruction::BitCast :
1881 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1882 return create(opcode, C, Ty, Name, InsertBefore);
1883}
1884
1885CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1886 const std::string &Name,
1887 BasicBlock *InsertAtEnd) {
1888 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1889 "Invalid cast");
1890 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1891 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1892 Instruction::CastOps opcode =
1893 (SrcBits == DstBits ? Instruction::BitCast :
1894 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1895 return create(opcode, C, Ty, Name, InsertAtEnd);
1896}
1897
Duncan Sands55e50902008-01-06 10:12:28 +00001898// Check whether it is valid to call getCastOpcode for these types.
1899// This routine must be kept in sync with getCastOpcode.
1900bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) {
1901 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
1902 return false;
1903
1904 if (SrcTy == DestTy)
1905 return true;
1906
1907 // Get the bit sizes, we'll need these
1908 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1909 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1910
1911 // Run through the possibilities ...
1912 if (DestTy->isInteger()) { // Casting to integral
1913 if (SrcTy->isInteger()) { // Casting from integral
1914 return true;
1915 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1916 return true;
1917 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1918 // Casting from vector
1919 return DestBits == PTy->getBitWidth();
1920 } else { // Casting from something else
1921 return isa<PointerType>(SrcTy);
1922 }
1923 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
1924 if (SrcTy->isInteger()) { // Casting from integral
1925 return true;
1926 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1927 return true;
1928 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
1929 // Casting from vector
1930 return DestBits == PTy->getBitWidth();
1931 } else { // Casting from something else
1932 return false;
1933 }
1934 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1935 // Casting to vector
1936 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
1937 // Casting from vector
1938 return DestPTy->getBitWidth() == SrcPTy->getBitWidth();
1939 } else { // Casting from something else
1940 return DestPTy->getBitWidth() == SrcBits;
1941 }
1942 } else if (isa<PointerType>(DestTy)) { // Casting to pointer
1943 if (isa<PointerType>(SrcTy)) { // Casting from pointer
1944 return true;
1945 } else if (SrcTy->isInteger()) { // Casting from integral
1946 return true;
1947 } else { // Casting from something else
1948 return false;
1949 }
1950 } else { // Casting to something else
1951 return false;
1952 }
1953}
1954
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001955// Provide a way to get a "cast" where the cast opcode is inferred from the
1956// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001957// logic in the castIsValid function below. This axiom should hold:
1958// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1959// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001960// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00001961// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001962Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001963CastInst::getCastOpcode(
1964 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001965 // Get the bit sizes, we'll need these
1966 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001967 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1968 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001969
Duncan Sands55e50902008-01-06 10:12:28 +00001970 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
1971 "Only first class types are castable!");
1972
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001974 if (DestTy->isInteger()) { // Casting to integral
1975 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001976 if (DestBits < SrcBits)
1977 return Trunc; // int -> smaller int
1978 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001979 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001980 return SExt; // signed -> SEXT
1981 else
1982 return ZExt; // unsigned -> ZEXT
1983 } else {
1984 return BitCast; // Same size, No-op cast
1985 }
1986 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001987 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001988 return FPToSI; // FP -> sint
1989 else
1990 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001991 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001992 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001993 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001994 return BitCast; // Same size, no-op cast
1995 } else {
1996 assert(isa<PointerType>(SrcTy) &&
1997 "Casting from a value that is not first-class type");
1998 return PtrToInt; // ptr -> int
1999 }
2000 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00002001 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002002 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003 return SIToFP; // sint -> FP
2004 else
2005 return UIToFP; // uint -> FP
2006 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
2007 if (DestBits < SrcBits) {
2008 return FPTrunc; // FP -> smaller FP
2009 } else if (DestBits > SrcBits) {
2010 return FPExt; // FP -> larger FP
2011 } else {
2012 return BitCast; // same size, no-op cast
2013 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002014 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002015 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002016 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002017 return BitCast; // same size, no-op cast
2018 } else {
2019 assert(0 && "Casting pointer or non-first class to float");
2020 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00002021 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
2022 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002023 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002024 "Casting vector to vector of different widths");
2025 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00002027 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002028 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00002029 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002030 }
2031 } else if (isa<PointerType>(DestTy)) {
2032 if (isa<PointerType>(SrcTy)) {
2033 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00002034 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002035 return IntToPtr; // int -> ptr
2036 } else {
2037 assert(!"Casting pointer to other than pointer or int");
2038 }
2039 } else {
2040 assert(!"Casting to type that is not first-class");
2041 }
2042
2043 // If we fall through to here we probably hit an assertion cast above
2044 // and assertions are not turned on. Anything we return is an error, so
2045 // BitCast is as good a choice as any.
2046 return BitCast;
2047}
2048
2049//===----------------------------------------------------------------------===//
2050// CastInst SubClass Constructors
2051//===----------------------------------------------------------------------===//
2052
2053/// Check that the construction parameters for a CastInst are correct. This
2054/// could be broken out into the separate constructors but it is useful to have
2055/// it in one place and to eliminate the redundant code for getting the sizes
2056/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002057bool
2058CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002059
2060 // Check for type sanity on the arguments
2061 const Type *SrcTy = S->getType();
2062 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
2063 return false;
2064
2065 // Get the size of the types in bits, we'll need this later
2066 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
2067 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
2068
2069 // Switch on the opcode provided
2070 switch (op) {
2071 default: return false; // This is an input error
2072 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00002073 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002074 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002075 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002076 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00002077 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002078 case Instruction::FPTrunc:
2079 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2080 SrcBitSize > DstBitSize;
2081 case Instruction::FPExt:
2082 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
2083 SrcBitSize < DstBitSize;
2084 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002085 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00002086 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2087 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2088 return SVTy->getElementType()->isInteger() &&
2089 DVTy->getElementType()->isFloatingPoint() &&
2090 SVTy->getNumElements() == DVTy->getNumElements();
2091 }
2092 }
Chris Lattner03c49532007-01-15 02:27:26 +00002093 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002094 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002095 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00002096 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
2097 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
2098 return SVTy->getElementType()->isFloatingPoint() &&
2099 DVTy->getElementType()->isInteger() &&
2100 SVTy->getNumElements() == DVTy->getNumElements();
2101 }
2102 }
Chris Lattner03c49532007-01-15 02:27:26 +00002103 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002104 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002105 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002106 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002107 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002108 case Instruction::BitCast:
2109 // BitCast implies a no-op cast of type only. No bits change.
2110 // However, you can't cast pointers to anything but pointers.
2111 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2112 return false;
2113
Duncan Sands55e50902008-01-06 10:12:28 +00002114 // Now we know we're not dealing with a pointer/non-pointer mismatch. In all
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115 // these cases, the cast is okay if the source and destination bit widths
2116 // are identical.
2117 return SrcBitSize == DstBitSize;
2118 }
2119}
2120
2121TruncInst::TruncInst(
2122 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2123) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002124 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002125}
2126
2127TruncInst::TruncInst(
2128 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2129) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002130 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002131}
2132
2133ZExtInst::ZExtInst(
2134 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2135) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002136 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002137}
2138
2139ZExtInst::ZExtInst(
2140 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2141) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002142 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002143}
2144SExtInst::SExtInst(
2145 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2146) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002147 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002148}
2149
Jeff Cohencc08c832006-12-02 02:22:01 +00002150SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002151 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2152) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002153 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002154}
2155
2156FPTruncInst::FPTruncInst(
2157 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2158) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002159 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002160}
2161
2162FPTruncInst::FPTruncInst(
2163 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2164) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002165 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002166}
2167
2168FPExtInst::FPExtInst(
2169 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2170) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002171 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002172}
2173
2174FPExtInst::FPExtInst(
2175 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2176) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002177 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002178}
2179
2180UIToFPInst::UIToFPInst(
2181 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2182) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002183 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002184}
2185
2186UIToFPInst::UIToFPInst(
2187 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2188) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002189 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002190}
2191
2192SIToFPInst::SIToFPInst(
2193 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2194) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002195 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002196}
2197
2198SIToFPInst::SIToFPInst(
2199 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2200) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002201 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002202}
2203
2204FPToUIInst::FPToUIInst(
2205 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2206) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002207 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002208}
2209
2210FPToUIInst::FPToUIInst(
2211 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2212) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002213 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002214}
2215
2216FPToSIInst::FPToSIInst(
2217 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2218) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002219 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002220}
2221
2222FPToSIInst::FPToSIInst(
2223 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2224) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002225 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002226}
2227
2228PtrToIntInst::PtrToIntInst(
2229 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2230) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002231 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002232}
2233
2234PtrToIntInst::PtrToIntInst(
2235 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2236) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002237 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002238}
2239
2240IntToPtrInst::IntToPtrInst(
2241 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2242) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002243 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002244}
2245
2246IntToPtrInst::IntToPtrInst(
2247 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2248) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002249 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002250}
2251
2252BitCastInst::BitCastInst(
2253 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2254) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002255 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002256}
2257
2258BitCastInst::BitCastInst(
2259 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2260) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002261 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002262}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002263
2264//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002265// CmpInst Classes
2266//===----------------------------------------------------------------------===//
2267
2268CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2269 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002270 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002271 Ops[0].init(LHS, this);
2272 Ops[1].init(RHS, this);
2273 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002274 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002275 if (op == Instruction::ICmp) {
2276 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2277 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2278 "Invalid ICmp predicate value");
2279 const Type* Op0Ty = getOperand(0)->getType();
2280 const Type* Op1Ty = getOperand(1)->getType();
2281 assert(Op0Ty == Op1Ty &&
2282 "Both operands to ICmp instruction are not of the same type!");
2283 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002284 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002285 "Invalid operand types for ICmp instruction");
2286 return;
2287 }
2288 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2289 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2290 "Invalid FCmp predicate value");
2291 const Type* Op0Ty = getOperand(0)->getType();
2292 const Type* Op1Ty = getOperand(1)->getType();
2293 assert(Op0Ty == Op1Ty &&
2294 "Both operands to FCmp instruction are not of the same type!");
2295 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002296 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002297 "Invalid operand types for FCmp instruction");
2298}
2299
2300CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2301 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002302 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002303 Ops[0].init(LHS, this);
2304 Ops[1].init(RHS, this);
2305 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002306 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002307 if (op == Instruction::ICmp) {
2308 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2309 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2310 "Invalid ICmp predicate value");
2311
2312 const Type* Op0Ty = getOperand(0)->getType();
2313 const Type* Op1Ty = getOperand(1)->getType();
2314 assert(Op0Ty == Op1Ty &&
2315 "Both operands to ICmp instruction are not of the same type!");
2316 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002317 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002318 "Invalid operand types for ICmp instruction");
2319 return;
2320 }
2321 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2322 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2323 "Invalid FCmp predicate value");
2324 const Type* Op0Ty = getOperand(0)->getType();
2325 const Type* Op1Ty = getOperand(1)->getType();
2326 assert(Op0Ty == Op1Ty &&
2327 "Both operands to FCmp instruction are not of the same type!");
2328 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002329 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002330 "Invalid operand types for FCmp instruction");
2331}
2332
2333CmpInst *
2334CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2335 const std::string &Name, Instruction *InsertBefore) {
2336 if (Op == Instruction::ICmp) {
2337 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2338 InsertBefore);
2339 }
2340 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2341 InsertBefore);
2342}
2343
2344CmpInst *
2345CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2346 const std::string &Name, BasicBlock *InsertAtEnd) {
2347 if (Op == Instruction::ICmp) {
2348 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2349 InsertAtEnd);
2350 }
2351 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2352 InsertAtEnd);
2353}
2354
2355void CmpInst::swapOperands() {
2356 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2357 IC->swapOperands();
2358 else
2359 cast<FCmpInst>(this)->swapOperands();
2360}
2361
2362bool CmpInst::isCommutative() {
2363 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2364 return IC->isCommutative();
2365 return cast<FCmpInst>(this)->isCommutative();
2366}
2367
2368bool CmpInst::isEquality() {
2369 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2370 return IC->isEquality();
2371 return cast<FCmpInst>(this)->isEquality();
2372}
2373
2374
2375ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2376 switch (pred) {
2377 default:
2378 assert(!"Unknown icmp predicate!");
2379 case ICMP_EQ: return ICMP_NE;
2380 case ICMP_NE: return ICMP_EQ;
2381 case ICMP_UGT: return ICMP_ULE;
2382 case ICMP_ULT: return ICMP_UGE;
2383 case ICMP_UGE: return ICMP_ULT;
2384 case ICMP_ULE: return ICMP_UGT;
2385 case ICMP_SGT: return ICMP_SLE;
2386 case ICMP_SLT: return ICMP_SGE;
2387 case ICMP_SGE: return ICMP_SLT;
2388 case ICMP_SLE: return ICMP_SGT;
2389 }
2390}
2391
2392ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2393 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002394 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002395 case ICMP_EQ: case ICMP_NE:
2396 return pred;
2397 case ICMP_SGT: return ICMP_SLT;
2398 case ICMP_SLT: return ICMP_SGT;
2399 case ICMP_SGE: return ICMP_SLE;
2400 case ICMP_SLE: return ICMP_SGE;
2401 case ICMP_UGT: return ICMP_ULT;
2402 case ICMP_ULT: return ICMP_UGT;
2403 case ICMP_UGE: return ICMP_ULE;
2404 case ICMP_ULE: return ICMP_UGE;
2405 }
2406}
2407
Reid Spencer266e42b2006-12-23 06:05:41 +00002408ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2409 switch (pred) {
2410 default: assert(! "Unknown icmp predicate!");
2411 case ICMP_EQ: case ICMP_NE:
2412 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2413 return pred;
2414 case ICMP_UGT: return ICMP_SGT;
2415 case ICMP_ULT: return ICMP_SLT;
2416 case ICMP_UGE: return ICMP_SGE;
2417 case ICMP_ULE: return ICMP_SLE;
2418 }
2419}
2420
2421bool ICmpInst::isSignedPredicate(Predicate pred) {
2422 switch (pred) {
2423 default: assert(! "Unknown icmp predicate!");
2424 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2425 return true;
2426 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2427 case ICMP_UGE: case ICMP_ULE:
2428 return false;
2429 }
2430}
2431
Reid Spencer0286bc12007-02-28 22:00:54 +00002432/// Initialize a set of values that all satisfy the condition with C.
2433///
2434ConstantRange
2435ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2436 APInt Lower(C);
2437 APInt Upper(C);
2438 uint32_t BitWidth = C.getBitWidth();
2439 switch (pred) {
2440 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2441 case ICmpInst::ICMP_EQ: Upper++; break;
2442 case ICmpInst::ICMP_NE: Lower++; break;
2443 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2444 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2445 case ICmpInst::ICMP_UGT:
2446 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2447 break;
2448 case ICmpInst::ICMP_SGT:
2449 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2450 break;
2451 case ICmpInst::ICMP_ULE:
2452 Lower = APInt::getMinValue(BitWidth); Upper++;
2453 break;
2454 case ICmpInst::ICMP_SLE:
2455 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2456 break;
2457 case ICmpInst::ICMP_UGE:
2458 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2459 break;
2460 case ICmpInst::ICMP_SGE:
2461 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2462 break;
2463 }
2464 return ConstantRange(Lower, Upper);
2465}
2466
Reid Spencerd9436b62006-11-20 01:22:35 +00002467FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2468 switch (pred) {
2469 default:
2470 assert(!"Unknown icmp predicate!");
2471 case FCMP_OEQ: return FCMP_UNE;
2472 case FCMP_ONE: return FCMP_UEQ;
2473 case FCMP_OGT: return FCMP_ULE;
2474 case FCMP_OLT: return FCMP_UGE;
2475 case FCMP_OGE: return FCMP_ULT;
2476 case FCMP_OLE: return FCMP_UGT;
2477 case FCMP_UEQ: return FCMP_ONE;
2478 case FCMP_UNE: return FCMP_OEQ;
2479 case FCMP_UGT: return FCMP_OLE;
2480 case FCMP_ULT: return FCMP_OGE;
2481 case FCMP_UGE: return FCMP_OLT;
2482 case FCMP_ULE: return FCMP_OGT;
2483 case FCMP_ORD: return FCMP_UNO;
2484 case FCMP_UNO: return FCMP_ORD;
2485 case FCMP_TRUE: return FCMP_FALSE;
2486 case FCMP_FALSE: return FCMP_TRUE;
2487 }
2488}
2489
2490FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2491 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002492 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002493 case FCMP_FALSE: case FCMP_TRUE:
2494 case FCMP_OEQ: case FCMP_ONE:
2495 case FCMP_UEQ: case FCMP_UNE:
2496 case FCMP_ORD: case FCMP_UNO:
2497 return pred;
2498 case FCMP_OGT: return FCMP_OLT;
2499 case FCMP_OLT: return FCMP_OGT;
2500 case FCMP_OGE: return FCMP_OLE;
2501 case FCMP_OLE: return FCMP_OGE;
2502 case FCMP_UGT: return FCMP_ULT;
2503 case FCMP_ULT: return FCMP_UGT;
2504 case FCMP_UGE: return FCMP_ULE;
2505 case FCMP_ULE: return FCMP_UGE;
2506 }
2507}
2508
Reid Spencer266e42b2006-12-23 06:05:41 +00002509bool CmpInst::isUnsigned(unsigned short predicate) {
2510 switch (predicate) {
2511 default: return false;
2512 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2513 case ICmpInst::ICMP_UGE: return true;
2514 }
2515}
2516
2517bool CmpInst::isSigned(unsigned short predicate){
2518 switch (predicate) {
2519 default: return false;
2520 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2521 case ICmpInst::ICMP_SGE: return true;
2522 }
2523}
2524
2525bool CmpInst::isOrdered(unsigned short predicate) {
2526 switch (predicate) {
2527 default: return false;
2528 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2529 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2530 case FCmpInst::FCMP_ORD: return true;
2531 }
2532}
2533
2534bool CmpInst::isUnordered(unsigned short predicate) {
2535 switch (predicate) {
2536 default: return false;
2537 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2538 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2539 case FCmpInst::FCMP_UNO: return true;
2540 }
2541}
2542
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002543//===----------------------------------------------------------------------===//
2544// SwitchInst Implementation
2545//===----------------------------------------------------------------------===//
2546
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002547void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002548 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002549 ReservedSpace = 2+NumCases*2;
2550 NumOperands = 2;
2551 OperandList = new Use[ReservedSpace];
2552
2553 OperandList[0].init(Value, this);
2554 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002555}
2556
Chris Lattner2195fc42007-02-24 00:55:48 +00002557/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2558/// switch on and a default destination. The number of additional cases can
2559/// be specified here to make memory allocation more efficient. This
2560/// constructor can also autoinsert before another instruction.
2561SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2562 Instruction *InsertBefore)
2563 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2564 init(Value, Default, NumCases);
2565}
2566
2567/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2568/// switch on and a default destination. The number of additional cases can
2569/// be specified here to make memory allocation more efficient. This
2570/// constructor also autoinserts at the end of the specified BasicBlock.
2571SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2572 BasicBlock *InsertAtEnd)
2573 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2574 init(Value, Default, NumCases);
2575}
2576
Misha Brukmanb1c93172005-04-21 23:48:37 +00002577SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002578 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2579 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002580 Use *OL = OperandList, *InOL = SI.OperandList;
2581 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2582 OL[i].init(InOL[i], this);
2583 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002584 }
2585}
2586
Gordon Henriksen14a55692007-12-10 02:14:30 +00002587SwitchInst::~SwitchInst() {
2588 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002589}
2590
2591
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002592/// addCase - Add an entry to the switch instruction...
2593///
Chris Lattner47ac1872005-02-24 05:32:09 +00002594void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002595 unsigned OpNo = NumOperands;
2596 if (OpNo+2 > ReservedSpace)
2597 resizeOperands(0); // Get more space!
2598 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002599 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002600 NumOperands = OpNo+2;
2601 OperandList[OpNo].init(OnVal, this);
2602 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002603}
2604
2605/// removeCase - This method removes the specified successor from the switch
2606/// instruction. Note that this cannot be used to remove the default
2607/// destination (successor #0).
2608///
2609void SwitchInst::removeCase(unsigned idx) {
2610 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002611 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2612
2613 unsigned NumOps = getNumOperands();
2614 Use *OL = OperandList;
2615
2616 // Move everything after this operand down.
2617 //
2618 // FIXME: we could just swap with the end of the list, then erase. However,
2619 // client might not expect this to happen. The code as it is thrashes the
2620 // use/def lists, which is kinda lame.
2621 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2622 OL[i-2] = OL[i];
2623 OL[i-2+1] = OL[i+1];
2624 }
2625
2626 // Nuke the last value.
2627 OL[NumOps-2].set(0);
2628 OL[NumOps-2+1].set(0);
2629 NumOperands = NumOps-2;
2630}
2631
2632/// resizeOperands - resize operands - This adjusts the length of the operands
2633/// list according to the following behavior:
2634/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2635/// of operation. This grows the number of ops by 1.5 times.
2636/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2637/// 3. If NumOps == NumOperands, trim the reserved space.
2638///
2639void SwitchInst::resizeOperands(unsigned NumOps) {
2640 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002641 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002642 } else if (NumOps*2 > NumOperands) {
2643 // No resize needed.
2644 if (ReservedSpace >= NumOps) return;
2645 } else if (NumOps == NumOperands) {
2646 if (ReservedSpace == NumOps) return;
2647 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002648 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002649 }
2650
2651 ReservedSpace = NumOps;
2652 Use *NewOps = new Use[NumOps];
2653 Use *OldOps = OperandList;
2654 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2655 NewOps[i].init(OldOps[i], this);
2656 OldOps[i].set(0);
2657 }
2658 delete [] OldOps;
2659 OperandList = NewOps;
2660}
2661
2662
2663BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2664 return getSuccessor(idx);
2665}
2666unsigned SwitchInst::getNumSuccessorsV() const {
2667 return getNumSuccessors();
2668}
2669void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2670 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002671}
Chris Lattnerf22be932004-10-15 23:52:53 +00002672
2673
2674// Define these methods here so vtables don't get emitted into every translation
2675// unit that uses these classes.
2676
2677GetElementPtrInst *GetElementPtrInst::clone() const {
2678 return new GetElementPtrInst(*this);
2679}
2680
2681BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002682 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002683}
2684
Chris Lattner0b490b02007-08-24 20:48:18 +00002685FCmpInst* FCmpInst::clone() const {
2686 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2687}
2688ICmpInst* ICmpInst::clone() const {
2689 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002690}
2691
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002692MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2693AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2694FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2695LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2696StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2697CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2698CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2699CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2700CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2701CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2702CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2703CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2704CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2705CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2706CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2707CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2708CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2709CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002710SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2711VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2712
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002713ExtractElementInst *ExtractElementInst::clone() const {
2714 return new ExtractElementInst(*this);
2715}
2716InsertElementInst *InsertElementInst::clone() const {
2717 return new InsertElementInst(*this);
2718}
2719ShuffleVectorInst *ShuffleVectorInst::clone() const {
2720 return new ShuffleVectorInst(*this);
2721}
Chris Lattnerf22be932004-10-15 23:52:53 +00002722PHINode *PHINode::clone() const { return new PHINode(*this); }
2723ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2724BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2725SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2726InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2727UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002728UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}