blob: 6c8a072c19749fa3836e177a1d513a8a12cbcf6c [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 Lattnerf7b6d312005-05-06 20:26:43 +000026unsigned CallSite::getCallingConv() const {
27 if (CallInst *CI = dyn_cast<CallInst>(I))
28 return CI->getCallingConv();
29 else
30 return cast<InvokeInst>(I)->getCallingConv();
31}
32void CallSite::setCallingConv(unsigned CC) {
33 if (CallInst *CI = dyn_cast<CallInst>(I))
34 CI->setCallingConv(CC);
35 else
36 cast<InvokeInst>(I)->setCallingConv(CC);
37}
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000038const ParamAttrsList* CallSite::getParamAttrs() const {
39 if (CallInst *CI = dyn_cast<CallInst>(I))
40 return CI->getParamAttrs();
41 else
42 return cast<InvokeInst>(I)->getParamAttrs();
43}
44void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
45 if (CallInst *CI = dyn_cast<CallInst>(I))
46 CI->setParamAttrs(PAL);
47 else
48 cast<InvokeInst>(I)->setParamAttrs(PAL);
49}
Duncan Sands5208d1a2007-11-28 17:07:01 +000050bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
51 if (CallInst *CI = dyn_cast<CallInst>(I))
52 return CI->paramHasAttr(i, attr);
53 else
54 return cast<InvokeInst>(I)->paramHasAttr(i, attr);
55}
Duncan Sands38ef3a82007-12-03 20:06:50 +000056bool CallSite::doesNotAccessMemory() const {
57 if (CallInst *CI = dyn_cast<CallInst>(I))
58 return CI->doesNotAccessMemory();
59 else
60 return cast<InvokeInst>(I)->doesNotAccessMemory();
61}
62bool CallSite::onlyReadsMemory() const {
63 if (CallInst *CI = dyn_cast<CallInst>(I))
64 return CI->onlyReadsMemory();
65 else
66 return cast<InvokeInst>(I)->onlyReadsMemory();
67}
Duncan Sands8e4847e2007-12-16 15:51:49 +000068bool CallSite::isNoUnwind() const {
69 if (CallInst *CI = dyn_cast<CallInst>(I))
70 return CI->isNoUnwind();
71 else
72 return cast<InvokeInst>(I)->isNoUnwind();
73}
Chris Lattnerf7b6d312005-05-06 20:26:43 +000074
Gordon Henriksen14a55692007-12-10 02:14:30 +000075//===----------------------------------------------------------------------===//
76// TerminatorInst Class
77//===----------------------------------------------------------------------===//
78
79// Out of line virtual method, so the vtable, etc has a home.
80TerminatorInst::~TerminatorInst() {
81}
82
83// Out of line virtual method, so the vtable, etc has a home.
84UnaryInstruction::~UnaryInstruction() {
85}
86
87
Chris Lattnerafdb3de2005-01-29 00:35:16 +000088//===----------------------------------------------------------------------===//
89// PHINode Class
90//===----------------------------------------------------------------------===//
91
92PHINode::PHINode(const PHINode &PN)
93 : Instruction(PN.getType(), Instruction::PHI,
94 new Use[PN.getNumOperands()], PN.getNumOperands()),
95 ReservedSpace(PN.getNumOperands()) {
96 Use *OL = OperandList;
97 for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
98 OL[i].init(PN.getOperand(i), this);
99 OL[i+1].init(PN.getOperand(i+1), this);
100 }
101}
102
Gordon Henriksen14a55692007-12-10 02:14:30 +0000103PHINode::~PHINode() {
104 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105}
106
107// removeIncomingValue - Remove an incoming value. This is useful if a
108// predecessor basic block is deleted.
109Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
110 unsigned NumOps = getNumOperands();
111 Use *OL = OperandList;
112 assert(Idx*2 < NumOps && "BB not in PHI node!");
113 Value *Removed = OL[Idx*2];
114
115 // Move everything after this operand down.
116 //
117 // FIXME: we could just swap with the end of the list, then erase. However,
118 // client might not expect this to happen. The code as it is thrashes the
119 // use/def lists, which is kinda lame.
120 for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
121 OL[i-2] = OL[i];
122 OL[i-2+1] = OL[i+1];
123 }
124
125 // Nuke the last value.
126 OL[NumOps-2].set(0);
127 OL[NumOps-2+1].set(0);
128 NumOperands = NumOps-2;
129
130 // If the PHI node is dead, because it has zero entries, nuke it now.
131 if (NumOps == 2 && DeletePHIIfEmpty) {
132 // If anyone is using this PHI, make them use a dummy value instead...
133 replaceAllUsesWith(UndefValue::get(getType()));
134 eraseFromParent();
135 }
136 return Removed;
137}
138
139/// resizeOperands - resize operands - This adjusts the length of the operands
140/// list according to the following behavior:
141/// 1. If NumOps == 0, grow the operand list in response to a push_back style
142/// of operation. This grows the number of ops by 1.5 times.
143/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
144/// 3. If NumOps == NumOperands, trim the reserved space.
145///
146void PHINode::resizeOperands(unsigned NumOps) {
147 if (NumOps == 0) {
148 NumOps = (getNumOperands())*3/2;
149 if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
150 } else if (NumOps*2 > NumOperands) {
151 // No resize needed.
152 if (ReservedSpace >= NumOps) return;
153 } else if (NumOps == NumOperands) {
154 if (ReservedSpace == NumOps) return;
155 } else {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000156 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000157 }
158
159 ReservedSpace = NumOps;
160 Use *NewOps = new Use[NumOps];
161 Use *OldOps = OperandList;
162 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
163 NewOps[i].init(OldOps[i], this);
164 OldOps[i].set(0);
165 }
166 delete [] OldOps;
167 OperandList = NewOps;
168}
169
Nate Begemanb3923212005-08-04 23:24:19 +0000170/// hasConstantValue - If the specified PHI node always merges together the same
171/// value, return the value, otherwise return null.
172///
Chris Lattner1d8b2482005-08-05 00:49:06 +0000173Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
Nate Begemanb3923212005-08-04 23:24:19 +0000174 // If the PHI node only has one incoming value, eliminate the PHI node...
175 if (getNumIncomingValues() == 1)
Chris Lattner6e709c12005-08-05 15:37:31 +0000176 if (getIncomingValue(0) != this) // not X = phi X
177 return getIncomingValue(0);
178 else
179 return UndefValue::get(getType()); // Self cycle is dead.
180
Nate Begemanb3923212005-08-04 23:24:19 +0000181 // Otherwise if all of the incoming values are the same for the PHI, replace
182 // the PHI node with the incoming value.
183 //
184 Value *InVal = 0;
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000185 bool HasUndefInput = false;
Nate Begemanb3923212005-08-04 23:24:19 +0000186 for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000187 if (isa<UndefValue>(getIncomingValue(i)))
188 HasUndefInput = true;
189 else if (getIncomingValue(i) != this) // Not the PHI node itself...
Nate Begemanb3923212005-08-04 23:24:19 +0000190 if (InVal && getIncomingValue(i) != InVal)
191 return 0; // Not the same, bail out.
192 else
193 InVal = getIncomingValue(i);
194
195 // The only case that could cause InVal to be null is if we have a PHI node
196 // that only has entries for itself. In this case, there is no entry into the
197 // loop, so kill the PHI.
198 //
199 if (InVal == 0) InVal = UndefValue::get(getType());
200
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000201 // If we have a PHI node like phi(X, undef, X), where X is defined by some
202 // instruction, we cannot always return X as the result of the PHI node. Only
203 // do this if X is not an instruction (thus it must dominate the PHI block),
204 // or if the client is prepared to deal with this possibility.
205 if (HasUndefInput && !AllowNonDominatingInstruction)
206 if (Instruction *IV = dyn_cast<Instruction>(InVal))
207 // If it's in the entry block, it dominates everything.
Dan Gohmandcb291f2007-03-22 16:38:57 +0000208 if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() ||
Chris Lattner37774af2005-08-05 01:03:27 +0000209 isa<InvokeInst>(IV))
Chris Lattnerbcd8d2c2005-08-05 01:00:58 +0000210 return 0; // Cannot guarantee that InVal dominates this PHINode.
211
Nate Begemanb3923212005-08-04 23:24:19 +0000212 // All of the incoming values are the same, return the value now.
213 return InVal;
214}
215
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000216
217//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000218// CallInst Implementation
219//===----------------------------------------------------------------------===//
220
Gordon Henriksen14a55692007-12-10 02:14:30 +0000221CallInst::~CallInst() {
222 delete [] OperandList;
223 if (ParamAttrs)
224 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000225}
226
Chris Lattner054ba2c2007-02-13 00:58:44 +0000227void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) {
Reid Spencer019c8862007-04-09 15:01:12 +0000228 ParamAttrs = 0;
Chris Lattner054ba2c2007-02-13 00:58:44 +0000229 NumOperands = NumParams+1;
230 Use *OL = OperandList = new Use[NumParams+1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000231 OL[0].init(Func, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000232
Misha Brukmanb1c93172005-04-21 23:48:37 +0000233 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000234 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000235 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236
Chris Lattner054ba2c2007-02-13 00:58:44 +0000237 assert((NumParams == FTy->getNumParams() ||
238 (FTy->isVarArg() && NumParams > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000239 "Calling a function with bad signature!");
Chris Lattner054ba2c2007-02-13 00:58:44 +0000240 for (unsigned i = 0; i != NumParams; ++i) {
Chris Lattner667a0562006-05-03 00:48:22 +0000241 assert((i >= FTy->getNumParams() ||
242 FTy->getParamType(i) == Params[i]->getType()) &&
243 "Calling a function with a bad signature!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000244 OL[i+1].init(Params[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000245 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000246}
247
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000248void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
Reid Spencer019c8862007-04-09 15:01:12 +0000249 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000250 NumOperands = 3;
251 Use *OL = OperandList = new Use[3];
252 OL[0].init(Func, this);
253 OL[1].init(Actual1, this);
254 OL[2].init(Actual2, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000255
256 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000257 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000258 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000259
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000260 assert((FTy->getNumParams() == 2 ||
Chris Lattner667a0562006-05-03 00:48:22 +0000261 (FTy->isVarArg() && FTy->getNumParams() < 2)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000262 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000263 assert((0 >= FTy->getNumParams() ||
264 FTy->getParamType(0) == Actual1->getType()) &&
265 "Calling a function with a bad signature!");
266 assert((1 >= FTy->getNumParams() ||
267 FTy->getParamType(1) == Actual2->getType()) &&
268 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269}
270
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000271void CallInst::init(Value *Func, Value *Actual) {
Reid Spencer019c8862007-04-09 15:01:12 +0000272 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000273 NumOperands = 2;
274 Use *OL = OperandList = new Use[2];
275 OL[0].init(Func, this);
276 OL[1].init(Actual, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000277
278 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000280 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000281
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000282 assert((FTy->getNumParams() == 1 ||
283 (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000284 "Calling a function with bad signature");
Chris Lattner667a0562006-05-03 00:48:22 +0000285 assert((0 == FTy->getNumParams() ||
286 FTy->getParamType(0) == Actual->getType()) &&
287 "Calling a function with a bad signature!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000288}
289
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000290void CallInst::init(Value *Func) {
Reid Spencer019c8862007-04-09 15:01:12 +0000291 ParamAttrs = 0;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000292 NumOperands = 1;
293 Use *OL = OperandList = new Use[1];
294 OL[0].init(Func, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000295
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000296 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000297 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000298 FTy = FTy; // silence warning.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000299
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000300 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000301}
302
David Greene17a5dfe2007-08-01 03:43:44 +0000303#if 0
304// Leave for llvm-gcc
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000305CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000306 const std::string &Name, BasicBlock *InsertAtEnd)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000307 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
David Greene17a5dfe2007-08-01 03:43:44 +0000308 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000309 Instruction::Call, 0, 0, InsertAtEnd) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000310 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000311 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000312}
313CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs,
314 const std::string &Name, Instruction *InsertBefore)
David Greene17a5dfe2007-08-01 03:43:44 +0000315 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
316 ->getElementType())->getReturnType(),
317 Instruction::Call, 0, 0, InsertBefore) {
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000318 init(Func, Args, NumArgs);
Chris Lattner2195fc42007-02-24 00:55:48 +0000319 setName(Name);
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000320}
321
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000322CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
323 const std::string &Name, Instruction *InsertBefore)
324 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
325 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000326 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000327 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000328 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000329}
330
331CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2,
332 const std::string &Name, BasicBlock *InsertAtEnd)
333 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
334 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000335 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000336 init(Func, Actual1, Actual2);
Chris Lattner2195fc42007-02-24 00:55:48 +0000337 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000338}
David Greene17a5dfe2007-08-01 03:43:44 +0000339#endif
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000340CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +0000341 Instruction *InsertBefore)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000342 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
343 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000344 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000345 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000346 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000347}
348
349CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name,
350 BasicBlock *InsertAtEnd)
351 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
352 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000353 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000354 init(Func, Actual);
Chris Lattner2195fc42007-02-24 00:55:48 +0000355 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000356}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000357CallInst::CallInst(Value *Func, const std::string &Name,
358 Instruction *InsertBefore)
359 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
360 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000361 Instruction::Call, 0, 0, InsertBefore) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000362 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000363 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000364}
365
366CallInst::CallInst(Value *Func, const std::string &Name,
367 BasicBlock *InsertAtEnd)
368 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
369 ->getElementType())->getReturnType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000370 Instruction::Call, 0, 0, InsertAtEnd) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000371 init(Func);
Chris Lattner2195fc42007-02-24 00:55:48 +0000372 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000373}
374
Misha Brukmanb1c93172005-04-21 23:48:37 +0000375CallInst::CallInst(const CallInst &CI)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000376 : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000377 CI.getNumOperands()),
378 ParamAttrs(0) {
379 setParamAttrs(CI.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000380 SubclassData = CI.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000381 Use *OL = OperandList;
382 Use *InOL = CI.OperandList;
383 for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
384 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000385}
386
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000387void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) {
388 if (ParamAttrs == newAttrs)
389 return;
390
Reid Spencerc6a83842007-04-22 17:28:03 +0000391 if (ParamAttrs)
392 ParamAttrs->dropRef();
393
394 if (newAttrs)
395 newAttrs->addRef();
396
397 ParamAttrs = newAttrs;
398}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000399
Duncan Sands5208d1a2007-11-28 17:07:01 +0000400bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
401 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
402 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000403 if (const Function *F = getCalledFunction())
404 return F->paramHasAttr(i, attr);
405 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000406}
407
Duncan Sands5208d1a2007-11-28 17:07:01 +0000408
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000409//===----------------------------------------------------------------------===//
410// InvokeInst Implementation
411//===----------------------------------------------------------------------===//
412
Gordon Henriksen14a55692007-12-10 02:14:30 +0000413InvokeInst::~InvokeInst() {
414 delete [] OperandList;
415 if (ParamAttrs)
416 ParamAttrs->dropRef();
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000417}
418
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000419void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000420 Value* const *Args, unsigned NumArgs) {
Reid Spencerce38beb2007-04-09 18:00:57 +0000421 ParamAttrs = 0;
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000422 NumOperands = 3+NumArgs;
423 Use *OL = OperandList = new Use[3+NumArgs];
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000424 OL[0].init(Fn, this);
425 OL[1].init(IfNormal, this);
426 OL[2].init(IfException, this);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000427 const FunctionType *FTy =
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000428 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000429 FTy = FTy; // silence warning.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000430
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000431 assert((NumArgs == FTy->getNumParams()) ||
432 (FTy->isVarArg() && NumArgs > FTy->getNumParams()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000433 "Calling a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000434
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000435 for (unsigned i = 0, e = NumArgs; i != e; i++) {
Chris Lattner667a0562006-05-03 00:48:22 +0000436 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000437 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000438 "Invoking a function with a bad signature!");
439
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000440 OL[i+3].init(Args[i], this);
Chris Lattner667a0562006-05-03 00:48:22 +0000441 }
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000442}
443
Misha Brukmanb1c93172005-04-21 23:48:37 +0000444InvokeInst::InvokeInst(const InvokeInst &II)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000445 : TerminatorInst(II.getType(), Instruction::Invoke,
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000446 new Use[II.getNumOperands()], II.getNumOperands()),
447 ParamAttrs(0) {
448 setParamAttrs(II.getParamAttrs());
Chris Lattnerf7b6d312005-05-06 20:26:43 +0000449 SubclassData = II.SubclassData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000450 Use *OL = OperandList, *InOL = II.OperandList;
451 for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
452 OL[i].init(InOL[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000453}
454
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000455BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
456 return getSuccessor(idx);
457}
458unsigned InvokeInst::getNumSuccessorsV() const {
459 return getNumSuccessors();
460}
461void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
462 return setSuccessor(idx, B);
463}
464
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000465void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) {
466 if (ParamAttrs == newAttrs)
467 return;
468
Reid Spencerc6a83842007-04-22 17:28:03 +0000469 if (ParamAttrs)
470 ParamAttrs->dropRef();
471
472 if (newAttrs)
473 newAttrs->addRef();
474
475 ParamAttrs = newAttrs;
476}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000477
Duncan Sands5208d1a2007-11-28 17:07:01 +0000478bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const {
479 if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr))
480 return true;
Duncan Sands8dfcd592007-11-29 08:30:15 +0000481 if (const Function *F = getCalledFunction())
482 return F->paramHasAttr(i, attr);
483 return false;
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000484}
485
Duncan Sands5208d1a2007-11-28 17:07:01 +0000486
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000487//===----------------------------------------------------------------------===//
488// ReturnInst Implementation
489//===----------------------------------------------------------------------===//
490
Chris Lattner2195fc42007-02-24 00:55:48 +0000491ReturnInst::ReturnInst(const ReturnInst &RI)
492 : TerminatorInst(Type::VoidTy, Instruction::Ret,
493 &RetVal, RI.getNumOperands()) {
494 if (RI.getNumOperands())
495 RetVal.init(RI.RetVal, this);
496}
497
498ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore)
499 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) {
500 init(retVal);
501}
502ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
503 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
504 init(retVal);
505}
506ReturnInst::ReturnInst(BasicBlock *InsertAtEnd)
507 : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) {
508}
509
510
511
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000512void ReturnInst::init(Value *retVal) {
513 if (retVal && retVal->getType() != Type::VoidTy) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000514 assert(!isa<BasicBlock>(retVal) &&
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000515 "Cannot return basic block. Probably using the incorrect ctor");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000516 NumOperands = 1;
517 RetVal.init(retVal, this);
Alkis Evlogimenos531e9012004-11-17 21:02:25 +0000518 }
519}
520
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000521unsigned ReturnInst::getNumSuccessorsV() const {
522 return getNumSuccessors();
523}
524
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000525// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
526// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000527void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000528 assert(0 && "ReturnInst has no successors!");
529}
530
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000531BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
532 assert(0 && "ReturnInst has no successors!");
533 abort();
534 return 0;
535}
536
537
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000538//===----------------------------------------------------------------------===//
539// UnwindInst Implementation
540//===----------------------------------------------------------------------===//
541
Chris Lattner2195fc42007-02-24 00:55:48 +0000542UnwindInst::UnwindInst(Instruction *InsertBefore)
543 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) {
544}
545UnwindInst::UnwindInst(BasicBlock *InsertAtEnd)
546 : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) {
547}
548
549
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000550unsigned UnwindInst::getNumSuccessorsV() const {
551 return getNumSuccessors();
552}
553
554void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000555 assert(0 && "UnwindInst has no successors!");
556}
557
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000558BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
559 assert(0 && "UnwindInst has no successors!");
560 abort();
561 return 0;
562}
563
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000564//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000565// UnreachableInst Implementation
566//===----------------------------------------------------------------------===//
567
Chris Lattner2195fc42007-02-24 00:55:48 +0000568UnreachableInst::UnreachableInst(Instruction *InsertBefore)
569 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) {
570}
571UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd)
572 : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) {
573}
574
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000575unsigned UnreachableInst::getNumSuccessorsV() const {
576 return getNumSuccessors();
577}
578
579void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
580 assert(0 && "UnwindInst has no successors!");
581}
582
583BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
584 assert(0 && "UnwindInst has no successors!");
585 abort();
586 return 0;
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000587}
588
589//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000590// BranchInst Implementation
591//===----------------------------------------------------------------------===//
592
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000593void BranchInst::AssertOK() {
594 if (isConditional())
Reid Spencer542964f2007-01-11 18:21:29 +0000595 assert(getCondition()->getType() == Type::Int1Ty &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000596 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000597}
598
Chris Lattner2195fc42007-02-24 00:55:48 +0000599BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
600 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) {
601 assert(IfTrue != 0 && "Branch destination may not be null!");
602 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
603}
604BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
605 Instruction *InsertBefore)
606: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) {
607 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
608 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
609 Ops[2].init(Cond, this);
610#ifndef NDEBUG
611 AssertOK();
612#endif
613}
614
615BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
616 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) {
617 assert(IfTrue != 0 && "Branch destination may not be null!");
618 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
619}
620
621BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
622 BasicBlock *InsertAtEnd)
623 : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) {
624 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
625 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
626 Ops[2].init(Cond, this);
627#ifndef NDEBUG
628 AssertOK();
629#endif
630}
631
632
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000633BranchInst::BranchInst(const BranchInst &BI) :
Chris Lattner2195fc42007-02-24 00:55:48 +0000634 TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000635 OperandList[0].init(BI.getOperand(0), this);
636 if (BI.getNumOperands() != 1) {
637 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
638 OperandList[1].init(BI.getOperand(1), this);
639 OperandList[2].init(BI.getOperand(2), this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000640 }
641}
642
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000643BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
644 return getSuccessor(idx);
645}
646unsigned BranchInst::getNumSuccessorsV() const {
647 return getNumSuccessors();
648}
649void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
650 setSuccessor(idx, B);
651}
652
653
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000654//===----------------------------------------------------------------------===//
655// AllocationInst Implementation
656//===----------------------------------------------------------------------===//
657
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000658static Value *getAISize(Value *Amt) {
659 if (!Amt)
Reid Spencer8d9336d2006-12-31 05:26:44 +0000660 Amt = ConstantInt::get(Type::Int32Ty, 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000661 else {
662 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +0000663 "Passed basic block into allocation size parameter! Use other ctor");
Reid Spencer8d9336d2006-12-31 05:26:44 +0000664 assert(Amt->getType() == Type::Int32Ty &&
Reid Spencer7e16e232007-01-26 06:30:34 +0000665 "Malloc/Allocation array size is not a 32-bit integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +0000666 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000667 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000668}
669
Misha Brukmanb1c93172005-04-21 23:48:37 +0000670AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000671 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000672 Instruction *InsertBefore)
Christopher Lambedf07882007-12-17 01:12:55 +0000673 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000674 InsertBefore), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000675 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000676 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000677 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000678}
679
Misha Brukmanb1c93172005-04-21 23:48:37 +0000680AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
Nate Begeman848622f2005-11-05 09:21:28 +0000681 unsigned Align, const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000682 BasicBlock *InsertAtEnd)
Christopher Lambedf07882007-12-17 01:12:55 +0000683 : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
Chris Lattner2195fc42007-02-24 00:55:48 +0000684 InsertAtEnd), Alignment(Align) {
Chris Lattner79b8c792005-11-05 21:57:54 +0000685 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000686 assert(Ty != Type::VoidTy && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +0000687 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000688}
689
Gordon Henriksen14a55692007-12-10 02:14:30 +0000690// Out of line virtual method, so the vtable, etc has a home.
691AllocationInst::~AllocationInst() {
692}
693
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000694bool AllocationInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +0000695 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
696 return CI->getZExtValue() != 1;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000697 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000698}
699
700const Type *AllocationInst::getAllocatedType() const {
701 return getType()->getElementType();
702}
703
704AllocaInst::AllocaInst(const AllocaInst &AI)
705 : AllocationInst(AI.getType()->getElementType(), (Value*)AI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000706 Instruction::Alloca, AI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000707}
708
709MallocInst::MallocInst(const MallocInst &MI)
710 : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0),
Nate Begeman848622f2005-11-05 09:21:28 +0000711 Instruction::Malloc, MI.getAlignment()) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000712}
713
714//===----------------------------------------------------------------------===//
715// FreeInst Implementation
716//===----------------------------------------------------------------------===//
717
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000718void FreeInst::AssertOK() {
719 assert(isa<PointerType>(getOperand(0)->getType()) &&
720 "Can not free something of nonpointer type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000721}
722
723FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000724 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000725 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000726}
727
728FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000729 : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000730 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000731}
732
733
734//===----------------------------------------------------------------------===//
735// LoadInst Implementation
736//===----------------------------------------------------------------------===//
737
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000738void LoadInst::AssertOK() {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000739 assert(isa<PointerType>(getOperand(0)->getType()) &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000740 "Ptr must have pointer type.");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000741}
742
743LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000744 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000745 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000746 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000747 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000748 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000749 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000750}
751
752LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000753 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000754 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000755 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000756 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000757 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000758 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000759}
760
761LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
762 Instruction *InsertBef)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000763 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000764 Load, Ptr, InsertBef) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000765 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000766 setAlignment(0);
767 AssertOK();
768 setName(Name);
769}
770
771LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
772 unsigned Align, Instruction *InsertBef)
773 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
774 Load, Ptr, InsertBef) {
775 setVolatile(isVolatile);
776 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000777 AssertOK();
Chris Lattner0f048162007-02-13 07:54:42 +0000778 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000779}
780
Dan Gohman68659282007-07-18 20:51:11 +0000781LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
782 unsigned Align, BasicBlock *InsertAE)
783 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
784 Load, Ptr, InsertAE) {
785 setVolatile(isVolatile);
786 setAlignment(Align);
787 AssertOK();
788 setName(Name);
789}
790
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000791LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
792 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000793 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000794 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000795 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000796 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000797 AssertOK();
798 setName(Name);
799}
800
801
802
803LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +0000804 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
805 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000806 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000807 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000808 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000809 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000810}
811
812LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000813 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
814 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +0000815 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000816 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000817 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000818 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000819}
820
821LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
822 Instruction *InsertBef)
823: UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +0000824 Load, Ptr, InsertBef) {
Chris Lattner0f048162007-02-13 07:54:42 +0000825 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000826 setAlignment(0);
Chris Lattner0f048162007-02-13 07:54:42 +0000827 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000828 if (Name && Name[0]) setName(Name);
Chris Lattner0f048162007-02-13 07:54:42 +0000829}
830
831LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
832 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +0000833 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
834 Load, Ptr, InsertAE) {
Chris Lattnerdf57a022005-02-05 01:38:38 +0000835 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000836 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000837 AssertOK();
Chris Lattner2195fc42007-02-24 00:55:48 +0000838 if (Name && Name[0]) setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000839}
840
Christopher Lamb84485702007-04-22 19:24:39 +0000841void LoadInst::setAlignment(unsigned Align) {
842 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
843 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
844}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000845
846//===----------------------------------------------------------------------===//
847// StoreInst Implementation
848//===----------------------------------------------------------------------===//
849
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000850void StoreInst::AssertOK() {
851 assert(isa<PointerType>(getOperand(1)->getType()) &&
852 "Ptr must have pointer type!");
853 assert(getOperand(0)->getType() ==
854 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +0000855 && "Ptr must be a pointer to Val type!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000856}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000857
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000858
859StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000860 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000861 Ops[0].init(val, this);
862 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000863 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000864 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000865 AssertOK();
866}
867
868StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000869 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000870 Ops[0].init(val, this);
871 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000872 setVolatile(false);
Christopher Lamb84485702007-04-22 19:24:39 +0000873 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000874 AssertOK();
875}
876
Misha Brukmanb1c93172005-04-21 23:48:37 +0000877StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000878 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +0000879 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000880 Ops[0].init(val, this);
881 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000882 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000883 setAlignment(0);
884 AssertOK();
885}
886
887StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
888 unsigned Align, Instruction *InsertBefore)
889 : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) {
890 Ops[0].init(val, this);
891 Ops[1].init(addr, this);
892 setVolatile(isVolatile);
893 setAlignment(Align);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000894 AssertOK();
895}
896
Misha Brukmanb1c93172005-04-21 23:48:37 +0000897StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +0000898 unsigned Align, BasicBlock *InsertAtEnd)
899 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
900 Ops[0].init(val, this);
901 Ops[1].init(addr, this);
902 setVolatile(isVolatile);
903 setAlignment(Align);
904 AssertOK();
905}
906
907StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000908 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +0000909 : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000910 Ops[0].init(val, this);
911 Ops[1].init(addr, this);
Chris Lattnerdf57a022005-02-05 01:38:38 +0000912 setVolatile(isVolatile);
Christopher Lamb84485702007-04-22 19:24:39 +0000913 setAlignment(0);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000914 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000915}
916
Christopher Lamb84485702007-04-22 19:24:39 +0000917void StoreInst::setAlignment(unsigned Align) {
918 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
919 SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1);
920}
921
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000922//===----------------------------------------------------------------------===//
923// GetElementPtrInst Implementation
924//===----------------------------------------------------------------------===//
925
Christopher Lambedf07882007-12-17 01:12:55 +0000926static unsigned retrieveAddrSpace(const Value *Val) {
927 return cast<PointerType>(Val->getType())->getAddressSpace();
928}
929
Chris Lattner79807c3d2007-01-31 19:47:18 +0000930void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) {
931 NumOperands = 1+NumIdx;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000932 Use *OL = OperandList = new Use[NumOperands];
933 OL[0].init(Ptr, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000934
Chris Lattner79807c3d2007-01-31 19:47:18 +0000935 for (unsigned i = 0; i != NumIdx; ++i)
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000936 OL[i+1].init(Idx[i], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000937}
938
Chris Lattner82981202005-05-03 05:43:30 +0000939void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
940 NumOperands = 2;
941 Use *OL = OperandList = new Use[2];
942 OL[0].init(Ptr, this);
943 OL[1].init(Idx, this);
944}
945
Chris Lattner82981202005-05-03 05:43:30 +0000946GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
947 const std::string &Name, Instruction *InBe)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000948 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +0000949 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +0000950 GetElementPtr, 0, 0, InBe) {
Chris Lattner82981202005-05-03 05:43:30 +0000951 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000952 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000953}
954
955GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
956 const std::string &Name, BasicBlock *IAE)
Christopher Lamb54dd24c2007-12-11 08:59:05 +0000957 : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)),
Christopher Lambedf07882007-12-17 01:12:55 +0000958 retrieveAddrSpace(Ptr)),
Chris Lattner2195fc42007-02-24 00:55:48 +0000959 GetElementPtr, 0, 0, IAE) {
Chris Lattner82981202005-05-03 05:43:30 +0000960 init(Ptr, Idx);
Chris Lattner2195fc42007-02-24 00:55:48 +0000961 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +0000962}
963
Gordon Henriksen14a55692007-12-10 02:14:30 +0000964GetElementPtrInst::~GetElementPtrInst() {
965 delete[] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000966}
967
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000968// getIndexedType - Returns the type of the element that would be loaded with
969// a load instruction with the specified parameters.
970//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000971// A null type is returned if the indices are invalid for the specified
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000972// pointer type.
973//
Misha Brukmanb1c93172005-04-21 23:48:37 +0000974const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Chris Lattner302116a2007-01-31 04:40:28 +0000975 Value* const *Idxs,
976 unsigned NumIdx,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000977 bool AllowCompositeLeaf) {
978 if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
979
980 // Handle the special case of the empty set index set...
Chris Lattner302116a2007-01-31 04:40:28 +0000981 if (NumIdx == 0)
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000982 if (AllowCompositeLeaf ||
983 cast<PointerType>(Ptr)->getElementType()->isFirstClassType())
984 return cast<PointerType>(Ptr)->getElementType();
985 else
986 return 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000987
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000988 unsigned CurIdx = 0;
989 while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
Chris Lattner302116a2007-01-31 04:40:28 +0000990 if (NumIdx == CurIdx) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000991 if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr;
992 return 0; // Can't load a whole structure or array!?!?
993 }
994
Chris Lattner302116a2007-01-31 04:40:28 +0000995 Value *Index = Idxs[CurIdx++];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000996 if (isa<PointerType>(CT) && CurIdx != 1)
997 return 0; // Can only index into pointer types at the first index!
998 if (!CT->indexValid(Index)) return 0;
999 Ptr = CT->getTypeAtIndex(Index);
1000
1001 // If the new type forwards to another type, then it is in the middle
1002 // of being refined to another type (and hence, may have dropped all
1003 // references to what it was using before). So, use the new forwarded
1004 // type.
1005 if (const Type * Ty = Ptr->getForwardedType()) {
1006 Ptr = Ty;
1007 }
1008 }
Chris Lattner302116a2007-01-31 04:40:28 +00001009 return CurIdx == NumIdx ? Ptr : 0;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001010}
1011
Chris Lattner82981202005-05-03 05:43:30 +00001012const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
1013 const PointerType *PTy = dyn_cast<PointerType>(Ptr);
1014 if (!PTy) return 0; // Type isn't a pointer type!
1015
1016 // Check the pointer index.
1017 if (!PTy->indexValid(Idx)) return 0;
1018
Chris Lattnerc2233332005-05-03 16:44:45 +00001019 return PTy->getElementType();
Chris Lattner82981202005-05-03 05:43:30 +00001020}
1021
Chris Lattner45f15572007-04-14 00:12:57 +00001022
1023/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1024/// zeros. If so, the result pointer and the first operand have the same
1025/// value, just potentially different types.
1026bool GetElementPtrInst::hasAllZeroIndices() const {
1027 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1028 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1029 if (!CI->isZero()) return false;
1030 } else {
1031 return false;
1032 }
1033 }
1034 return true;
1035}
1036
Chris Lattner27058292007-04-27 20:35:56 +00001037/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1038/// constant integers. If so, the result pointer and the first operand have
1039/// a constant offset between them.
1040bool GetElementPtrInst::hasAllConstantIndices() const {
1041 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1042 if (!isa<ConstantInt>(getOperand(i)))
1043 return false;
1044 }
1045 return true;
1046}
1047
Chris Lattner45f15572007-04-14 00:12:57 +00001048
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001049//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001050// ExtractElementInst Implementation
1051//===----------------------------------------------------------------------===//
1052
1053ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001054 const std::string &Name,
1055 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001056 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001057 ExtractElement, Ops, 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001058 assert(isValidOperands(Val, Index) &&
1059 "Invalid extractelement instruction operands!");
Robert Bocchino23004482006-01-10 19:05:34 +00001060 Ops[0].init(Val, this);
1061 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001062 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001063}
1064
Chris Lattner65511ff2006-10-05 06:24:58 +00001065ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1066 const std::string &Name,
1067 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001068 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001069 ExtractElement, Ops, 2, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001070 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001071 assert(isValidOperands(Val, Index) &&
1072 "Invalid extractelement instruction operands!");
1073 Ops[0].init(Val, this);
1074 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001075 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001076}
1077
1078
Robert Bocchino23004482006-01-10 19:05:34 +00001079ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001080 const std::string &Name,
1081 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001082 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001083 ExtractElement, Ops, 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001084 assert(isValidOperands(Val, Index) &&
1085 "Invalid extractelement instruction operands!");
1086
Robert Bocchino23004482006-01-10 19:05:34 +00001087 Ops[0].init(Val, this);
1088 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001089 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001090}
1091
Chris Lattner65511ff2006-10-05 06:24:58 +00001092ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV,
1093 const std::string &Name,
1094 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001095 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001096 ExtractElement, Ops, 2, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001097 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001098 assert(isValidOperands(Val, Index) &&
1099 "Invalid extractelement instruction operands!");
1100
1101 Ops[0].init(Val, this);
1102 Ops[1].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001103 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001104}
1105
1106
Chris Lattner54865b32006-04-08 04:05:48 +00001107bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001108 if (!isa<VectorType>(Val->getType()) || Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001109 return false;
1110 return true;
1111}
1112
1113
Robert Bocchino23004482006-01-10 19:05:34 +00001114//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001115// InsertElementInst Implementation
1116//===----------------------------------------------------------------------===//
1117
Chris Lattner0875d942006-04-14 22:20:32 +00001118InsertElementInst::InsertElementInst(const InsertElementInst &IE)
1119 : Instruction(IE.getType(), InsertElement, Ops, 3) {
1120 Ops[0].init(IE.Ops[0], this);
1121 Ops[1].init(IE.Ops[1], this);
1122 Ops[2].init(IE.Ops[2], this);
1123}
Chris Lattner54865b32006-04-08 04:05:48 +00001124InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001125 const std::string &Name,
1126 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001127 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001128 assert(isValidOperands(Vec, Elt, Index) &&
1129 "Invalid insertelement instruction operands!");
1130 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001131 Ops[1].init(Elt, this);
1132 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001133 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001134}
1135
Chris Lattner65511ff2006-10-05 06:24:58 +00001136InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1137 const std::string &Name,
1138 Instruction *InsertBef)
Chris Lattner2195fc42007-02-24 00:55:48 +00001139 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001140 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001141 assert(isValidOperands(Vec, Elt, Index) &&
1142 "Invalid insertelement instruction operands!");
1143 Ops[0].init(Vec, this);
1144 Ops[1].init(Elt, this);
1145 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001146 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001147}
1148
1149
Chris Lattner54865b32006-04-08 04:05:48 +00001150InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001151 const std::string &Name,
1152 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001153 : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001154 assert(isValidOperands(Vec, Elt, Index) &&
1155 "Invalid insertelement instruction operands!");
1156
1157 Ops[0].init(Vec, this);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001158 Ops[1].init(Elt, this);
1159 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001160 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001161}
1162
Chris Lattner65511ff2006-10-05 06:24:58 +00001163InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV,
1164 const std::string &Name,
1165 BasicBlock *InsertAE)
Chris Lattner2195fc42007-02-24 00:55:48 +00001166: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) {
Reid Spencer8d9336d2006-12-31 05:26:44 +00001167 Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV);
Chris Lattner65511ff2006-10-05 06:24:58 +00001168 assert(isValidOperands(Vec, Elt, Index) &&
1169 "Invalid insertelement instruction operands!");
1170
1171 Ops[0].init(Vec, this);
1172 Ops[1].init(Elt, this);
1173 Ops[2].init(Index, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001174 setName(Name);
Chris Lattner65511ff2006-10-05 06:24:58 +00001175}
1176
Chris Lattner54865b32006-04-08 04:05:48 +00001177bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1178 const Value *Index) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001179 if (!isa<VectorType>(Vec->getType()))
Reid Spencer09575ba2007-02-15 03:39:18 +00001180 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001181
Reid Spencerd84d35b2007-02-15 02:26:10 +00001182 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001183 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001184
Reid Spencer8d9336d2006-12-31 05:26:44 +00001185 if (Index->getType() != Type::Int32Ty)
Chris Lattner54865b32006-04-08 04:05:48 +00001186 return false; // Third operand of insertelement must be uint.
1187 return true;
1188}
1189
1190
Robert Bocchinoca27f032006-01-17 20:07:22 +00001191//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001192// ShuffleVectorInst Implementation
1193//===----------------------------------------------------------------------===//
1194
Chris Lattner0875d942006-04-14 22:20:32 +00001195ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV)
1196 : Instruction(SV.getType(), ShuffleVector, Ops, 3) {
1197 Ops[0].init(SV.Ops[0], this);
1198 Ops[1].init(SV.Ops[1], this);
1199 Ops[2].init(SV.Ops[2], this);
1200}
1201
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001202ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1203 const std::string &Name,
1204 Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00001205 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001206 assert(isValidOperands(V1, V2, Mask) &&
1207 "Invalid shuffle vector instruction operands!");
1208 Ops[0].init(V1, this);
1209 Ops[1].init(V2, this);
1210 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001211 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001212}
1213
1214ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1215 const std::string &Name,
1216 BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00001217 : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001218 assert(isValidOperands(V1, V2, Mask) &&
1219 "Invalid shuffle vector instruction operands!");
1220
1221 Ops[0].init(V1, this);
1222 Ops[1].init(V2, this);
1223 Ops[2].init(Mask, this);
Chris Lattner2195fc42007-02-24 00:55:48 +00001224 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001225}
1226
1227bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
1228 const Value *Mask) {
Reid Spencerd84d35b2007-02-15 02:26:10 +00001229 if (!isa<VectorType>(V1->getType())) return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001230 if (V1->getType() != V2->getType()) return false;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001231 if (!isa<VectorType>(Mask->getType()) ||
1232 cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
1233 cast<VectorType>(Mask->getType())->getNumElements() !=
1234 cast<VectorType>(V1->getType())->getNumElements())
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001235 return false;
1236 return true;
1237}
1238
1239
1240//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001241// BinaryOperator Class
1242//===----------------------------------------------------------------------===//
1243
Chris Lattner2195fc42007-02-24 00:55:48 +00001244BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1245 const Type *Ty, const std::string &Name,
1246 Instruction *InsertBefore)
1247 : Instruction(Ty, iType, Ops, 2, InsertBefore) {
1248 Ops[0].init(S1, this);
1249 Ops[1].init(S2, this);
1250 init(iType);
1251 setName(Name);
1252}
1253
1254BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
1255 const Type *Ty, const std::string &Name,
1256 BasicBlock *InsertAtEnd)
1257 : Instruction(Ty, iType, Ops, 2, InsertAtEnd) {
1258 Ops[0].init(S1, this);
1259 Ops[1].init(S2, this);
1260 init(iType);
1261 setName(Name);
1262}
1263
1264
1265void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001266 Value *LHS = getOperand(0), *RHS = getOperand(1);
Chris Lattnerf14c76c2007-02-01 04:59:37 +00001267 LHS = LHS; RHS = RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001268 assert(LHS->getType() == RHS->getType() &&
1269 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001270#ifndef NDEBUG
1271 switch (iType) {
1272 case Add: case Sub:
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001273 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001274 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001275 "Arithmetic operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001276 assert((getType()->isInteger() || getType()->isFloatingPoint() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001277 isa<VectorType>(getType())) &&
Brian Gaeke02209042004-08-20 06:00:58 +00001278 "Tried to create an arithmetic operation on a non-arithmetic type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001279 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001280 case UDiv:
1281 case SDiv:
1282 assert(getType() == LHS->getType() &&
1283 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001284 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1285 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001286 "Incorrect operand type (not integer) for S/UDIV");
1287 break;
1288 case FDiv:
1289 assert(getType() == LHS->getType() &&
1290 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001291 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1292 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001293 && "Incorrect operand type (not floating point) for FDIV");
1294 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001295 case URem:
1296 case SRem:
1297 assert(getType() == LHS->getType() &&
1298 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001299 assert((getType()->isInteger() || (isa<VectorType>(getType()) &&
1300 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001301 "Incorrect operand type (not integer) for S/UREM");
1302 break;
1303 case FRem:
1304 assert(getType() == LHS->getType() &&
1305 "Arithmetic operation should return same type as operands!");
Reid Spencerd84d35b2007-02-15 02:26:10 +00001306 assert((getType()->isFloatingPoint() || (isa<VectorType>(getType()) &&
1307 cast<VectorType>(getType())->getElementType()->isFloatingPoint()))
Reid Spencer7eb55b32006-11-02 01:53:59 +00001308 && "Incorrect operand type (not floating point) for FREM");
1309 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001310 case Shl:
1311 case LShr:
1312 case AShr:
1313 assert(getType() == LHS->getType() &&
1314 "Shift operation should return same type as operands!");
1315 assert(getType()->isInteger() &&
1316 "Shift operation requires integer operands");
1317 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001318 case And: case Or:
1319 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001320 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001321 "Logical operation should return same type as operands!");
Chris Lattner03c49532007-01-15 02:27:26 +00001322 assert((getType()->isInteger() ||
Reid Spencerd84d35b2007-02-15 02:26:10 +00001323 (isa<VectorType>(getType()) &&
1324 cast<VectorType>(getType())->getElementType()->isInteger())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00001325 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001326 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001327 default:
1328 break;
1329 }
1330#endif
1331}
1332
1333BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001334 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001335 Instruction *InsertBefore) {
1336 assert(S1->getType() == S2->getType() &&
1337 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00001338 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001339}
1340
1341BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
Misha Brukman96eb8782005-03-16 05:42:00 +00001342 const std::string &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001343 BasicBlock *InsertAtEnd) {
1344 BinaryOperator *Res = create(Op, S1, S2, Name);
1345 InsertAtEnd->getInstList().push_back(Res);
1346 return Res;
1347}
1348
1349BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1350 Instruction *InsertBefore) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001351 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1352 return new BinaryOperator(Instruction::Sub,
1353 zero, Op,
1354 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001355}
1356
1357BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name,
1358 BasicBlock *InsertAtEnd) {
Reid Spencer2eadb532007-01-21 00:29:26 +00001359 Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType());
1360 return new BinaryOperator(Instruction::Sub,
1361 zero, Op,
1362 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001363}
1364
1365BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1366 Instruction *InsertBefore) {
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001367 Constant *C;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001368 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001369 C = ConstantInt::getAllOnesValue(PTy->getElementType());
Reid Spencerd84d35b2007-02-15 02:26:10 +00001370 C = ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), C));
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001371 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001372 C = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00001373 }
1374
1375 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001376 Op->getType(), Name, InsertBefore);
1377}
1378
1379BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name,
1380 BasicBlock *InsertAtEnd) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001381 Constant *AllOnes;
Reid Spencerd84d35b2007-02-15 02:26:10 +00001382 if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001383 // Create a vector of all ones values.
Zhou Sheng75b871f2007-01-11 12:24:14 +00001384 Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001385 AllOnes =
Reid Spencerd84d35b2007-02-15 02:26:10 +00001386 ConstantVector::get(std::vector<Constant*>(PTy->getNumElements(), Elt));
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001387 } else {
Zhou Sheng75b871f2007-01-11 12:24:14 +00001388 AllOnes = ConstantInt::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00001389 }
1390
1391 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001392 Op->getType(), Name, InsertAtEnd);
1393}
1394
1395
1396// isConstantAllOnes - Helper function for several functions below
1397static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner1edec382007-06-15 06:04:24 +00001398 if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
1399 return CI->isAllOnesValue();
1400 if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
1401 return CV->isAllOnesValue();
1402 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001403}
1404
1405bool BinaryOperator::isNeg(const Value *V) {
1406 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1407 if (Bop->getOpcode() == Instruction::Sub)
Reid Spencer2eadb532007-01-21 00:29:26 +00001408 return Bop->getOperand(0) ==
1409 ConstantExpr::getZeroValueForNegationExpr(Bop->getType());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001410 return false;
1411}
1412
1413bool BinaryOperator::isNot(const Value *V) {
1414 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
1415 return (Bop->getOpcode() == Instruction::Xor &&
1416 (isConstantAllOnes(Bop->getOperand(1)) ||
1417 isConstantAllOnes(Bop->getOperand(0))));
1418 return false;
1419}
1420
Chris Lattner2c7d1772005-04-24 07:28:37 +00001421Value *BinaryOperator::getNegArgument(Value *BinOp) {
1422 assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
1423 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001424}
1425
Chris Lattner2c7d1772005-04-24 07:28:37 +00001426const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
1427 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001428}
1429
Chris Lattner2c7d1772005-04-24 07:28:37 +00001430Value *BinaryOperator::getNotArgument(Value *BinOp) {
1431 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
1432 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
1433 Value *Op0 = BO->getOperand(0);
1434 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001435 if (isConstantAllOnes(Op0)) return Op1;
1436
1437 assert(isConstantAllOnes(Op1));
1438 return Op0;
1439}
1440
Chris Lattner2c7d1772005-04-24 07:28:37 +00001441const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
1442 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001443}
1444
1445
1446// swapOperands - Exchange the two operands to this instruction. This
1447// instruction is safe to use on any binary instruction and does not
1448// modify the semantics of the instruction. If the instruction is
1449// order dependent (SetLT f.e.) the opcode is changed.
1450//
1451bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00001452 if (!isCommutative())
1453 return true; // Can't commute operands
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001454 std::swap(Ops[0], Ops[1]);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001455 return false;
1456}
1457
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001458//===----------------------------------------------------------------------===//
1459// CastInst Class
1460//===----------------------------------------------------------------------===//
1461
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001462// Just determine if this cast only deals with integral->integral conversion.
1463bool CastInst::isIntegerCast() const {
1464 switch (getOpcode()) {
1465 default: return false;
1466 case Instruction::ZExt:
1467 case Instruction::SExt:
1468 case Instruction::Trunc:
1469 return true;
1470 case Instruction::BitCast:
Chris Lattner03c49532007-01-15 02:27:26 +00001471 return getOperand(0)->getType()->isInteger() && getType()->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001472 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00001473}
1474
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001475bool CastInst::isLosslessCast() const {
1476 // Only BitCast can be lossless, exit fast if we're not BitCast
1477 if (getOpcode() != Instruction::BitCast)
1478 return false;
1479
1480 // Identity cast is always lossless
1481 const Type* SrcTy = getOperand(0)->getType();
1482 const Type* DstTy = getType();
1483 if (SrcTy == DstTy)
1484 return true;
1485
Reid Spencer8d9336d2006-12-31 05:26:44 +00001486 // Pointer to pointer is always lossless.
1487 if (isa<PointerType>(SrcTy))
1488 return isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001489 return false; // Other types have no identity values
1490}
1491
1492/// This function determines if the CastInst does not require any bits to be
1493/// changed in order to effect the cast. Essentially, it identifies cases where
1494/// no code gen is necessary for the cast, hence the name no-op cast. For
1495/// example, the following are all no-op casts:
1496/// # bitcast uint %X, int
1497/// # bitcast uint* %x, sbyte*
Dan Gohmanfead7972007-05-11 21:43:24 +00001498/// # bitcast vector< 2 x int > %x, vector< 4 x short>
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001499/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only
1500/// @brief Determine if a cast is a no-op.
1501bool CastInst::isNoopCast(const Type *IntPtrTy) const {
1502 switch (getOpcode()) {
1503 default:
1504 assert(!"Invalid CastOp");
1505 case Instruction::Trunc:
1506 case Instruction::ZExt:
1507 case Instruction::SExt:
1508 case Instruction::FPTrunc:
1509 case Instruction::FPExt:
1510 case Instruction::UIToFP:
1511 case Instruction::SIToFP:
1512 case Instruction::FPToUI:
1513 case Instruction::FPToSI:
1514 return false; // These always modify bits
1515 case Instruction::BitCast:
1516 return true; // BitCast never modifies bits.
1517 case Instruction::PtrToInt:
1518 return IntPtrTy->getPrimitiveSizeInBits() ==
1519 getType()->getPrimitiveSizeInBits();
1520 case Instruction::IntToPtr:
1521 return IntPtrTy->getPrimitiveSizeInBits() ==
1522 getOperand(0)->getType()->getPrimitiveSizeInBits();
1523 }
1524}
1525
1526/// This function determines if a pair of casts can be eliminated and what
1527/// opcode should be used in the elimination. This assumes that there are two
1528/// instructions like this:
1529/// * %F = firstOpcode SrcTy %x to MidTy
1530/// * %S = secondOpcode MidTy %F to DstTy
1531/// The function returns a resultOpcode so these two casts can be replaced with:
1532/// * %Replacement = resultOpcode %SrcTy %x to DstTy
1533/// If no such cast is permited, the function returns 0.
1534unsigned CastInst::isEliminableCastPair(
1535 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
1536 const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy)
1537{
1538 // Define the 144 possibilities for these two cast instructions. The values
1539 // in this matrix determine what to do in a given situation and select the
1540 // case in the switch below. The rows correspond to firstOp, the columns
1541 // correspond to secondOp. In looking at the table below, keep in mind
1542 // the following cast properties:
1543 //
1544 // Size Compare Source Destination
1545 // Operator Src ? Size Type Sign Type Sign
1546 // -------- ------------ ------------------- ---------------------
1547 // TRUNC > Integer Any Integral Any
1548 // ZEXT < Integral Unsigned Integer Any
1549 // SEXT < Integral Signed Integer Any
1550 // FPTOUI n/a FloatPt n/a Integral Unsigned
1551 // FPTOSI n/a FloatPt n/a Integral Signed
1552 // UITOFP n/a Integral Unsigned FloatPt n/a
1553 // SITOFP n/a Integral Signed FloatPt n/a
1554 // FPTRUNC > FloatPt n/a FloatPt n/a
1555 // FPEXT < FloatPt n/a FloatPt n/a
1556 // PTRTOINT n/a Pointer n/a Integral Unsigned
1557 // INTTOPTR n/a Integral Unsigned Pointer n/a
1558 // BITCONVERT = FirstClass n/a FirstClass n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00001559 //
1560 // NOTE: some transforms are safe, but we consider them to be non-profitable.
1561 // For example, we could merge "fptoui double to uint" + "zext uint to ulong",
1562 // into "fptoui double to ulong", but this loses information about the range
1563 // of the produced value (we no longer know the top-part is all zeros).
1564 // Further this conversion is often much more expensive for typical hardware,
1565 // and causes issues when building libgcc. We disallow fptosi+sext for the
1566 // same reason.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001567 const unsigned numCastOps =
1568 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
1569 static const uint8_t CastResults[numCastOps][numCastOps] = {
1570 // T F F U S F F P I B -+
1571 // R Z S P P I I T P 2 N T |
1572 // U E E 2 2 2 2 R E I T C +- secondOp
1573 // N X X U S F F N X N 2 V |
1574 // C T T I I P P C T T P T -+
1575 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+
1576 { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt |
1577 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt |
Chris Lattner6f6b4972006-12-05 23:43:59 +00001578 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI |
1579 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI |
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001580 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp
1581 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP |
1582 { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc |
1583 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt |
1584 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt |
1585 { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr |
1586 { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+
1587 };
1588
1589 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
1590 [secondOp-Instruction::CastOpsBegin];
1591 switch (ElimCase) {
1592 case 0:
1593 // categorically disallowed
1594 return 0;
1595 case 1:
1596 // allowed, use first cast's opcode
1597 return firstOp;
1598 case 2:
1599 // allowed, use second cast's opcode
1600 return secondOp;
1601 case 3:
1602 // no-op cast in second op implies firstOp as long as the DestTy
1603 // is integer
Chris Lattner03c49532007-01-15 02:27:26 +00001604 if (DstTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001605 return firstOp;
1606 return 0;
1607 case 4:
1608 // no-op cast in second op implies firstOp as long as the DestTy
1609 // is floating point
1610 if (DstTy->isFloatingPoint())
1611 return firstOp;
1612 return 0;
1613 case 5:
1614 // no-op cast in first op implies secondOp as long as the SrcTy
1615 // is an integer
Chris Lattner03c49532007-01-15 02:27:26 +00001616 if (SrcTy->isInteger())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001617 return secondOp;
1618 return 0;
1619 case 6:
1620 // no-op cast in first op implies secondOp as long as the SrcTy
1621 // is a floating point
1622 if (SrcTy->isFloatingPoint())
1623 return secondOp;
1624 return 0;
1625 case 7: {
1626 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size
1627 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1628 unsigned MidSize = MidTy->getPrimitiveSizeInBits();
1629 if (MidSize >= PtrSize)
1630 return Instruction::BitCast;
1631 return 0;
1632 }
1633 case 8: {
1634 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
1635 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
1636 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
1637 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1638 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1639 if (SrcSize == DstSize)
1640 return Instruction::BitCast;
1641 else if (SrcSize < DstSize)
1642 return firstOp;
1643 return secondOp;
1644 }
1645 case 9: // zext, sext -> zext, because sext can't sign extend after zext
1646 return Instruction::ZExt;
1647 case 10:
1648 // fpext followed by ftrunc is allowed if the bit size returned to is
1649 // the same as the original, in which case its just a bitcast
1650 if (SrcTy == DstTy)
1651 return Instruction::BitCast;
1652 return 0; // If the types are not the same we can't eliminate it.
1653 case 11:
1654 // bitcast followed by ptrtoint is allowed as long as the bitcast
1655 // is a pointer to pointer cast.
1656 if (isa<PointerType>(SrcTy) && isa<PointerType>(MidTy))
1657 return secondOp;
1658 return 0;
1659 case 12:
1660 // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast
1661 if (isa<PointerType>(MidTy) && isa<PointerType>(DstTy))
1662 return firstOp;
1663 return 0;
1664 case 13: {
1665 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
1666 unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits();
1667 unsigned SrcSize = SrcTy->getPrimitiveSizeInBits();
1668 unsigned DstSize = DstTy->getPrimitiveSizeInBits();
1669 if (SrcSize <= PtrSize && SrcSize == DstSize)
1670 return Instruction::BitCast;
1671 return 0;
1672 }
1673 case 99:
1674 // cast combination can't happen (error in input). This is for all cases
1675 // where the MidTy is not the same for the two cast instructions.
1676 assert(!"Invalid Cast Combination");
1677 return 0;
1678 default:
1679 assert(!"Error in CastResults table!!!");
1680 return 0;
1681 }
1682 return 0;
1683}
1684
1685CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1686 const std::string &Name, Instruction *InsertBefore) {
1687 // Construct and return the appropriate CastInst subclass
1688 switch (op) {
1689 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
1690 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
1691 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
1692 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
1693 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
1694 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
1695 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
1696 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
1697 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
1698 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
1699 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
1700 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
1701 default:
1702 assert(!"Invalid opcode provided");
1703 }
1704 return 0;
1705}
1706
1707CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty,
1708 const std::string &Name, BasicBlock *InsertAtEnd) {
1709 // Construct and return the appropriate CastInst subclass
1710 switch (op) {
1711 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
1712 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
1713 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
1714 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
1715 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
1716 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
1717 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
1718 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
1719 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
1720 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
1721 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
1722 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
1723 default:
1724 assert(!"Invalid opcode provided");
1725 }
1726 return 0;
1727}
1728
Reid Spencer5c140882006-12-04 20:17:56 +00001729CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1730 const std::string &Name,
1731 Instruction *InsertBefore) {
1732 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1733 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1734 return create(Instruction::ZExt, S, Ty, Name, InsertBefore);
1735}
1736
1737CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty,
1738 const std::string &Name,
1739 BasicBlock *InsertAtEnd) {
1740 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1741 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1742 return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
1743}
1744
1745CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1746 const std::string &Name,
1747 Instruction *InsertBefore) {
1748 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1749 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1750 return create(Instruction::SExt, S, Ty, Name, InsertBefore);
1751}
1752
1753CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty,
1754 const std::string &Name,
1755 BasicBlock *InsertAtEnd) {
1756 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1757 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1758 return create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
1759}
1760
1761CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1762 const std::string &Name,
1763 Instruction *InsertBefore) {
1764 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1765 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1766 return create(Instruction::Trunc, S, Ty, Name, InsertBefore);
1767}
1768
1769CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty,
1770 const std::string &Name,
1771 BasicBlock *InsertAtEnd) {
1772 if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
1773 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1774 return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
1775}
1776
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001777CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1778 const std::string &Name,
1779 BasicBlock *InsertAtEnd) {
1780 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001781 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001782 "Invalid cast");
1783
Chris Lattner03c49532007-01-15 02:27:26 +00001784 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001785 return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
1786 return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
1787}
1788
1789/// @brief Create a BitCast or a PtrToInt cast instruction
1790CastInst *CastInst::createPointerCast(Value *S, const Type *Ty,
1791 const std::string &Name,
1792 Instruction *InsertBefore) {
1793 assert(isa<PointerType>(S->getType()) && "Invalid cast");
Chris Lattner03c49532007-01-15 02:27:26 +00001794 assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001795 "Invalid cast");
1796
Chris Lattner03c49532007-01-15 02:27:26 +00001797 if (Ty->isInteger())
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00001798 return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
1799 return create(Instruction::BitCast, S, Ty, Name, InsertBefore);
1800}
1801
Reid Spencer7e933472006-12-12 00:49:44 +00001802CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1803 bool isSigned, const std::string &Name,
1804 Instruction *InsertBefore) {
Chris Lattner03c49532007-01-15 02:27:26 +00001805 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001806 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1807 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1808 Instruction::CastOps opcode =
1809 (SrcBits == DstBits ? Instruction::BitCast :
1810 (SrcBits > DstBits ? Instruction::Trunc :
1811 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1812 return create(opcode, C, Ty, Name, InsertBefore);
1813}
1814
1815CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty,
1816 bool isSigned, const std::string &Name,
1817 BasicBlock *InsertAtEnd) {
Chris Lattner03c49532007-01-15 02:27:26 +00001818 assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast");
Reid Spencer7e933472006-12-12 00:49:44 +00001819 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1820 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1821 Instruction::CastOps opcode =
1822 (SrcBits == DstBits ? Instruction::BitCast :
1823 (SrcBits > DstBits ? Instruction::Trunc :
1824 (isSigned ? Instruction::SExt : Instruction::ZExt)));
1825 return create(opcode, C, Ty, Name, InsertAtEnd);
1826}
1827
1828CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1829 const std::string &Name,
1830 Instruction *InsertBefore) {
1831 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1832 "Invalid cast");
1833 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1834 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1835 Instruction::CastOps opcode =
1836 (SrcBits == DstBits ? Instruction::BitCast :
1837 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1838 return create(opcode, C, Ty, Name, InsertBefore);
1839}
1840
1841CastInst *CastInst::createFPCast(Value *C, const Type *Ty,
1842 const std::string &Name,
1843 BasicBlock *InsertAtEnd) {
1844 assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() &&
1845 "Invalid cast");
1846 unsigned SrcBits = C->getType()->getPrimitiveSizeInBits();
1847 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1848 Instruction::CastOps opcode =
1849 (SrcBits == DstBits ? Instruction::BitCast :
1850 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
1851 return create(opcode, C, Ty, Name, InsertAtEnd);
1852}
1853
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001854// Provide a way to get a "cast" where the cast opcode is inferred from the
1855// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001856// logic in the castIsValid function below. This axiom should hold:
1857// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
1858// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001859// casting opcode for the arguments passed to it.
1860Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00001861CastInst::getCastOpcode(
1862 const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001863 // Get the bit sizes, we'll need these
1864 const Type *SrcTy = Src->getType();
Dan Gohmanfead7972007-05-11 21:43:24 +00001865 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
1866 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001867
1868 // Run through the possibilities ...
Chris Lattner03c49532007-01-15 02:27:26 +00001869 if (DestTy->isInteger()) { // Casting to integral
1870 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001871 if (DestBits < SrcBits)
1872 return Trunc; // int -> smaller int
1873 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00001874 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001875 return SExt; // signed -> SEXT
1876 else
1877 return ZExt; // unsigned -> ZEXT
1878 } else {
1879 return BitCast; // Same size, No-op cast
1880 }
1881 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00001882 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001883 return FPToSI; // FP -> sint
1884 else
1885 return FPToUI; // FP -> uint
Reid Spencerd84d35b2007-02-15 02:26:10 +00001886 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001887 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001888 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001889 return BitCast; // Same size, no-op cast
1890 } else {
1891 assert(isa<PointerType>(SrcTy) &&
1892 "Casting from a value that is not first-class type");
1893 return PtrToInt; // ptr -> int
1894 }
1895 } else if (DestTy->isFloatingPoint()) { // Casting to floating pt
Chris Lattner03c49532007-01-15 02:27:26 +00001896 if (SrcTy->isInteger()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00001897 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001898 return SIToFP; // sint -> FP
1899 else
1900 return UIToFP; // uint -> FP
1901 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
1902 if (DestBits < SrcBits) {
1903 return FPTrunc; // FP -> smaller FP
1904 } else if (DestBits > SrcBits) {
1905 return FPExt; // FP -> larger FP
1906 } else {
1907 return BitCast; // same size, no-op cast
1908 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001909 } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001910 assert(DestBits == PTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001911 "Casting vector to floating point of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001912 return BitCast; // same size, no-op cast
1913 } else {
1914 assert(0 && "Casting pointer or non-first class to float");
1915 }
Reid Spencerd84d35b2007-02-15 02:26:10 +00001916 } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
1917 if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001918 assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() &&
Dan Gohmanfead7972007-05-11 21:43:24 +00001919 "Casting vector to vector of different widths");
1920 return BitCast; // vector -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001921 } else if (DestPTy->getBitWidth() == SrcBits) {
Dan Gohmanfead7972007-05-11 21:43:24 +00001922 return BitCast; // float/int -> vector
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001923 } else {
Dan Gohmanfead7972007-05-11 21:43:24 +00001924 assert(!"Illegal cast to vector (wrong type or size)");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001925 }
1926 } else if (isa<PointerType>(DestTy)) {
1927 if (isa<PointerType>(SrcTy)) {
1928 return BitCast; // ptr -> ptr
Chris Lattner03c49532007-01-15 02:27:26 +00001929 } else if (SrcTy->isInteger()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001930 return IntToPtr; // int -> ptr
1931 } else {
1932 assert(!"Casting pointer to other than pointer or int");
1933 }
1934 } else {
1935 assert(!"Casting to type that is not first-class");
1936 }
1937
1938 // If we fall through to here we probably hit an assertion cast above
1939 // and assertions are not turned on. Anything we return is an error, so
1940 // BitCast is as good a choice as any.
1941 return BitCast;
1942}
1943
1944//===----------------------------------------------------------------------===//
1945// CastInst SubClass Constructors
1946//===----------------------------------------------------------------------===//
1947
1948/// Check that the construction parameters for a CastInst are correct. This
1949/// could be broken out into the separate constructors but it is useful to have
1950/// it in one place and to eliminate the redundant code for getting the sizes
1951/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00001952bool
1953CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001954
1955 // Check for type sanity on the arguments
1956 const Type *SrcTy = S->getType();
1957 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType())
1958 return false;
1959
1960 // Get the size of the types in bits, we'll need this later
1961 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1962 unsigned DstBitSize = DstTy->getPrimitiveSizeInBits();
1963
1964 // Switch on the opcode provided
1965 switch (op) {
1966 default: return false; // This is an input error
1967 case Instruction::Trunc:
Chris Lattner03c49532007-01-15 02:27:26 +00001968 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001969 case Instruction::ZExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001970 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001971 case Instruction::SExt:
Chris Lattner03c49532007-01-15 02:27:26 +00001972 return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001973 case Instruction::FPTrunc:
1974 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1975 SrcBitSize > DstBitSize;
1976 case Instruction::FPExt:
1977 return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() &&
1978 SrcBitSize < DstBitSize;
1979 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001980 case Instruction::SIToFP:
Nate Begemand4d45c22007-11-17 03:58:34 +00001981 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1982 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1983 return SVTy->getElementType()->isInteger() &&
1984 DVTy->getElementType()->isFloatingPoint() &&
1985 SVTy->getNumElements() == DVTy->getNumElements();
1986 }
1987 }
Chris Lattner03c49532007-01-15 02:27:26 +00001988 return SrcTy->isInteger() && DstTy->isFloatingPoint();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001989 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001990 case Instruction::FPToSI:
Nate Begemand4d45c22007-11-17 03:58:34 +00001991 if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) {
1992 if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) {
1993 return SVTy->getElementType()->isFloatingPoint() &&
1994 DVTy->getElementType()->isInteger() &&
1995 SVTy->getNumElements() == DVTy->getNumElements();
1996 }
1997 }
Chris Lattner03c49532007-01-15 02:27:26 +00001998 return SrcTy->isFloatingPoint() && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001999 case Instruction::PtrToInt:
Chris Lattner03c49532007-01-15 02:27:26 +00002000 return isa<PointerType>(SrcTy) && DstTy->isInteger();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002001 case Instruction::IntToPtr:
Chris Lattner03c49532007-01-15 02:27:26 +00002002 return SrcTy->isInteger() && isa<PointerType>(DstTy);
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002003 case Instruction::BitCast:
2004 // BitCast implies a no-op cast of type only. No bits change.
2005 // However, you can't cast pointers to anything but pointers.
2006 if (isa<PointerType>(SrcTy) != isa<PointerType>(DstTy))
2007 return false;
2008
2009 // Now we know we're not dealing with a pointer/non-poiner mismatch. In all
2010 // these cases, the cast is okay if the source and destination bit widths
2011 // are identical.
2012 return SrcBitSize == DstBitSize;
2013 }
2014}
2015
2016TruncInst::TruncInst(
2017 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2018) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002019 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002020}
2021
2022TruncInst::TruncInst(
2023 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2024) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002025 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002026}
2027
2028ZExtInst::ZExtInst(
2029 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2030) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002031 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002032}
2033
2034ZExtInst::ZExtInst(
2035 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2036) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002037 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002038}
2039SExtInst::SExtInst(
2040 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2041) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002042 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002043}
2044
Jeff Cohencc08c832006-12-02 02:22:01 +00002045SExtInst::SExtInst(
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002046 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2047) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002048 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002049}
2050
2051FPTruncInst::FPTruncInst(
2052 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2053) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002054 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002055}
2056
2057FPTruncInst::FPTruncInst(
2058 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2059) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002060 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002061}
2062
2063FPExtInst::FPExtInst(
2064 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2065) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002066 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002067}
2068
2069FPExtInst::FPExtInst(
2070 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2071) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002072 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002073}
2074
2075UIToFPInst::UIToFPInst(
2076 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2077) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002078 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002079}
2080
2081UIToFPInst::UIToFPInst(
2082 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2083) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002084 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002085}
2086
2087SIToFPInst::SIToFPInst(
2088 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2089) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002090 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002091}
2092
2093SIToFPInst::SIToFPInst(
2094 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2095) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002096 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002097}
2098
2099FPToUIInst::FPToUIInst(
2100 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2101) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002102 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002103}
2104
2105FPToUIInst::FPToUIInst(
2106 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2107) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002108 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002109}
2110
2111FPToSIInst::FPToSIInst(
2112 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2113) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002114 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002115}
2116
2117FPToSIInst::FPToSIInst(
2118 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2119) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002120 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002121}
2122
2123PtrToIntInst::PtrToIntInst(
2124 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2125) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002126 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002127}
2128
2129PtrToIntInst::PtrToIntInst(
2130 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2131) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002132 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002133}
2134
2135IntToPtrInst::IntToPtrInst(
2136 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2137) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002138 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002139}
2140
2141IntToPtrInst::IntToPtrInst(
2142 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2143) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002144 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002145}
2146
2147BitCastInst::BitCastInst(
2148 Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore
2149) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002150 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002151}
2152
2153BitCastInst::BitCastInst(
2154 Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd
2155) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002156 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002157}
Chris Lattnerf16dc002006-09-17 19:29:56 +00002158
2159//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00002160// CmpInst Classes
2161//===----------------------------------------------------------------------===//
2162
2163CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2164 const std::string &Name, Instruction *InsertBefore)
Chris Lattner2195fc42007-02-24 00:55:48 +00002165 : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002166 Ops[0].init(LHS, this);
2167 Ops[1].init(RHS, this);
2168 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002169 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002170 if (op == Instruction::ICmp) {
2171 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2172 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2173 "Invalid ICmp predicate value");
2174 const Type* Op0Ty = getOperand(0)->getType();
2175 const Type* Op1Ty = getOperand(1)->getType();
2176 assert(Op0Ty == Op1Ty &&
2177 "Both operands to ICmp instruction are not of the same type!");
2178 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002179 assert((Op0Ty->isInteger() || isa<PointerType>(Op0Ty)) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002180 "Invalid operand types for ICmp instruction");
2181 return;
2182 }
2183 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2184 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2185 "Invalid FCmp predicate value");
2186 const Type* Op0Ty = getOperand(0)->getType();
2187 const Type* Op1Ty = getOperand(1)->getType();
2188 assert(Op0Ty == Op1Ty &&
2189 "Both operands to FCmp instruction are not of the same type!");
2190 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002191 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002192 "Invalid operand types for FCmp instruction");
2193}
2194
2195CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS,
2196 const std::string &Name, BasicBlock *InsertAtEnd)
Chris Lattner2195fc42007-02-24 00:55:48 +00002197 : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00002198 Ops[0].init(LHS, this);
2199 Ops[1].init(RHS, this);
2200 SubclassData = predicate;
Reid Spencer871a9ea2007-04-11 13:04:48 +00002201 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00002202 if (op == Instruction::ICmp) {
2203 assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE &&
2204 predicate <= ICmpInst::LAST_ICMP_PREDICATE &&
2205 "Invalid ICmp predicate value");
2206
2207 const Type* Op0Ty = getOperand(0)->getType();
2208 const Type* Op1Ty = getOperand(1)->getType();
2209 assert(Op0Ty == Op1Ty &&
2210 "Both operands to ICmp instruction are not of the same type!");
2211 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002212 assert(Op0Ty->isInteger() || isa<PointerType>(Op0Ty) &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002213 "Invalid operand types for ICmp instruction");
2214 return;
2215 }
2216 assert(op == Instruction::FCmp && "Invalid CmpInst opcode");
2217 assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE &&
2218 "Invalid FCmp predicate value");
2219 const Type* Op0Ty = getOperand(0)->getType();
2220 const Type* Op1Ty = getOperand(1)->getType();
2221 assert(Op0Ty == Op1Ty &&
2222 "Both operands to FCmp instruction are not of the same type!");
2223 // Check that the operands are the right type
Reid Spencer2eadb532007-01-21 00:29:26 +00002224 assert(Op0Ty->isFloatingPoint() &&
Reid Spencerd9436b62006-11-20 01:22:35 +00002225 "Invalid operand types for FCmp instruction");
2226}
2227
2228CmpInst *
2229CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2230 const std::string &Name, Instruction *InsertBefore) {
2231 if (Op == Instruction::ICmp) {
2232 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2233 InsertBefore);
2234 }
2235 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2236 InsertBefore);
2237}
2238
2239CmpInst *
2240CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2,
2241 const std::string &Name, BasicBlock *InsertAtEnd) {
2242 if (Op == Instruction::ICmp) {
2243 return new ICmpInst(ICmpInst::Predicate(predicate), S1, S2, Name,
2244 InsertAtEnd);
2245 }
2246 return new FCmpInst(FCmpInst::Predicate(predicate), S1, S2, Name,
2247 InsertAtEnd);
2248}
2249
2250void CmpInst::swapOperands() {
2251 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2252 IC->swapOperands();
2253 else
2254 cast<FCmpInst>(this)->swapOperands();
2255}
2256
2257bool CmpInst::isCommutative() {
2258 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2259 return IC->isCommutative();
2260 return cast<FCmpInst>(this)->isCommutative();
2261}
2262
2263bool CmpInst::isEquality() {
2264 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
2265 return IC->isEquality();
2266 return cast<FCmpInst>(this)->isEquality();
2267}
2268
2269
2270ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) {
2271 switch (pred) {
2272 default:
2273 assert(!"Unknown icmp predicate!");
2274 case ICMP_EQ: return ICMP_NE;
2275 case ICMP_NE: return ICMP_EQ;
2276 case ICMP_UGT: return ICMP_ULE;
2277 case ICMP_ULT: return ICMP_UGE;
2278 case ICMP_UGE: return ICMP_ULT;
2279 case ICMP_ULE: return ICMP_UGT;
2280 case ICMP_SGT: return ICMP_SLE;
2281 case ICMP_SLT: return ICMP_SGE;
2282 case ICMP_SGE: return ICMP_SLT;
2283 case ICMP_SLE: return ICMP_SGT;
2284 }
2285}
2286
2287ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) {
2288 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002289 default: assert(! "Unknown icmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002290 case ICMP_EQ: case ICMP_NE:
2291 return pred;
2292 case ICMP_SGT: return ICMP_SLT;
2293 case ICMP_SLT: return ICMP_SGT;
2294 case ICMP_SGE: return ICMP_SLE;
2295 case ICMP_SLE: return ICMP_SGE;
2296 case ICMP_UGT: return ICMP_ULT;
2297 case ICMP_ULT: return ICMP_UGT;
2298 case ICMP_UGE: return ICMP_ULE;
2299 case ICMP_ULE: return ICMP_UGE;
2300 }
2301}
2302
Reid Spencer266e42b2006-12-23 06:05:41 +00002303ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
2304 switch (pred) {
2305 default: assert(! "Unknown icmp predicate!");
2306 case ICMP_EQ: case ICMP_NE:
2307 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2308 return pred;
2309 case ICMP_UGT: return ICMP_SGT;
2310 case ICMP_ULT: return ICMP_SLT;
2311 case ICMP_UGE: return ICMP_SGE;
2312 case ICMP_ULE: return ICMP_SLE;
2313 }
2314}
2315
2316bool ICmpInst::isSignedPredicate(Predicate pred) {
2317 switch (pred) {
2318 default: assert(! "Unknown icmp predicate!");
2319 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
2320 return true;
2321 case ICMP_EQ: case ICMP_NE: case ICMP_UGT: case ICMP_ULT:
2322 case ICMP_UGE: case ICMP_ULE:
2323 return false;
2324 }
2325}
2326
Reid Spencer0286bc12007-02-28 22:00:54 +00002327/// Initialize a set of values that all satisfy the condition with C.
2328///
2329ConstantRange
2330ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
2331 APInt Lower(C);
2332 APInt Upper(C);
2333 uint32_t BitWidth = C.getBitWidth();
2334 switch (pred) {
2335 default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!");
2336 case ICmpInst::ICMP_EQ: Upper++; break;
2337 case ICmpInst::ICMP_NE: Lower++; break;
2338 case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break;
2339 case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break;
2340 case ICmpInst::ICMP_UGT:
2341 Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2342 break;
2343 case ICmpInst::ICMP_SGT:
2344 Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2345 break;
2346 case ICmpInst::ICMP_ULE:
2347 Lower = APInt::getMinValue(BitWidth); Upper++;
2348 break;
2349 case ICmpInst::ICMP_SLE:
2350 Lower = APInt::getSignedMinValue(BitWidth); Upper++;
2351 break;
2352 case ICmpInst::ICMP_UGE:
2353 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
2354 break;
2355 case ICmpInst::ICMP_SGE:
2356 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
2357 break;
2358 }
2359 return ConstantRange(Lower, Upper);
2360}
2361
Reid Spencerd9436b62006-11-20 01:22:35 +00002362FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) {
2363 switch (pred) {
2364 default:
2365 assert(!"Unknown icmp predicate!");
2366 case FCMP_OEQ: return FCMP_UNE;
2367 case FCMP_ONE: return FCMP_UEQ;
2368 case FCMP_OGT: return FCMP_ULE;
2369 case FCMP_OLT: return FCMP_UGE;
2370 case FCMP_OGE: return FCMP_ULT;
2371 case FCMP_OLE: return FCMP_UGT;
2372 case FCMP_UEQ: return FCMP_ONE;
2373 case FCMP_UNE: return FCMP_OEQ;
2374 case FCMP_UGT: return FCMP_OLE;
2375 case FCMP_ULT: return FCMP_OGE;
2376 case FCMP_UGE: return FCMP_OLT;
2377 case FCMP_ULE: return FCMP_OGT;
2378 case FCMP_ORD: return FCMP_UNO;
2379 case FCMP_UNO: return FCMP_ORD;
2380 case FCMP_TRUE: return FCMP_FALSE;
2381 case FCMP_FALSE: return FCMP_TRUE;
2382 }
2383}
2384
2385FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) {
2386 switch (pred) {
Reid Spencer266e42b2006-12-23 06:05:41 +00002387 default: assert(!"Unknown fcmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00002388 case FCMP_FALSE: case FCMP_TRUE:
2389 case FCMP_OEQ: case FCMP_ONE:
2390 case FCMP_UEQ: case FCMP_UNE:
2391 case FCMP_ORD: case FCMP_UNO:
2392 return pred;
2393 case FCMP_OGT: return FCMP_OLT;
2394 case FCMP_OLT: return FCMP_OGT;
2395 case FCMP_OGE: return FCMP_OLE;
2396 case FCMP_OLE: return FCMP_OGE;
2397 case FCMP_UGT: return FCMP_ULT;
2398 case FCMP_ULT: return FCMP_UGT;
2399 case FCMP_UGE: return FCMP_ULE;
2400 case FCMP_ULE: return FCMP_UGE;
2401 }
2402}
2403
Reid Spencer266e42b2006-12-23 06:05:41 +00002404bool CmpInst::isUnsigned(unsigned short predicate) {
2405 switch (predicate) {
2406 default: return false;
2407 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
2408 case ICmpInst::ICMP_UGE: return true;
2409 }
2410}
2411
2412bool CmpInst::isSigned(unsigned short predicate){
2413 switch (predicate) {
2414 default: return false;
2415 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
2416 case ICmpInst::ICMP_SGE: return true;
2417 }
2418}
2419
2420bool CmpInst::isOrdered(unsigned short predicate) {
2421 switch (predicate) {
2422 default: return false;
2423 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
2424 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
2425 case FCmpInst::FCMP_ORD: return true;
2426 }
2427}
2428
2429bool CmpInst::isUnordered(unsigned short predicate) {
2430 switch (predicate) {
2431 default: return false;
2432 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
2433 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
2434 case FCmpInst::FCMP_UNO: return true;
2435 }
2436}
2437
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002438//===----------------------------------------------------------------------===//
2439// SwitchInst Implementation
2440//===----------------------------------------------------------------------===//
2441
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002442void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002443 assert(Value && Default);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002444 ReservedSpace = 2+NumCases*2;
2445 NumOperands = 2;
2446 OperandList = new Use[ReservedSpace];
2447
2448 OperandList[0].init(Value, this);
2449 OperandList[1].init(Default, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002450}
2451
Chris Lattner2195fc42007-02-24 00:55:48 +00002452/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2453/// switch on and a default destination. The number of additional cases can
2454/// be specified here to make memory allocation more efficient. This
2455/// constructor can also autoinsert before another instruction.
2456SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2457 Instruction *InsertBefore)
2458 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) {
2459 init(Value, Default, NumCases);
2460}
2461
2462/// SwitchInst ctor - Create a new switch instruction, specifying a value to
2463/// switch on and a default destination. The number of additional cases can
2464/// be specified here to make memory allocation more efficient. This
2465/// constructor also autoinserts at the end of the specified BasicBlock.
2466SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2467 BasicBlock *InsertAtEnd)
2468 : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) {
2469 init(Value, Default, NumCases);
2470}
2471
Misha Brukmanb1c93172005-04-21 23:48:37 +00002472SwitchInst::SwitchInst(const SwitchInst &SI)
Chris Lattner2195fc42007-02-24 00:55:48 +00002473 : TerminatorInst(Type::VoidTy, Instruction::Switch,
2474 new Use[SI.getNumOperands()], SI.getNumOperands()) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002475 Use *OL = OperandList, *InOL = SI.OperandList;
2476 for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
2477 OL[i].init(InOL[i], this);
2478 OL[i+1].init(InOL[i+1], this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002479 }
2480}
2481
Gordon Henriksen14a55692007-12-10 02:14:30 +00002482SwitchInst::~SwitchInst() {
2483 delete [] OperandList;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002484}
2485
2486
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002487/// addCase - Add an entry to the switch instruction...
2488///
Chris Lattner47ac1872005-02-24 05:32:09 +00002489void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002490 unsigned OpNo = NumOperands;
2491 if (OpNo+2 > ReservedSpace)
2492 resizeOperands(0); // Get more space!
2493 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002494 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002495 NumOperands = OpNo+2;
2496 OperandList[OpNo].init(OnVal, this);
2497 OperandList[OpNo+1].init(Dest, this);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002498}
2499
2500/// removeCase - This method removes the specified successor from the switch
2501/// instruction. Note that this cannot be used to remove the default
2502/// destination (successor #0).
2503///
2504void SwitchInst::removeCase(unsigned idx) {
2505 assert(idx != 0 && "Cannot remove the default case!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002506 assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
2507
2508 unsigned NumOps = getNumOperands();
2509 Use *OL = OperandList;
2510
2511 // Move everything after this operand down.
2512 //
2513 // FIXME: we could just swap with the end of the list, then erase. However,
2514 // client might not expect this to happen. The code as it is thrashes the
2515 // use/def lists, which is kinda lame.
2516 for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
2517 OL[i-2] = OL[i];
2518 OL[i-2+1] = OL[i+1];
2519 }
2520
2521 // Nuke the last value.
2522 OL[NumOps-2].set(0);
2523 OL[NumOps-2+1].set(0);
2524 NumOperands = NumOps-2;
2525}
2526
2527/// resizeOperands - resize operands - This adjusts the length of the operands
2528/// list according to the following behavior:
2529/// 1. If NumOps == 0, grow the operand list in response to a push_back style
2530/// of operation. This grows the number of ops by 1.5 times.
2531/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
2532/// 3. If NumOps == NumOperands, trim the reserved space.
2533///
2534void SwitchInst::resizeOperands(unsigned NumOps) {
2535 if (NumOps == 0) {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002536 NumOps = getNumOperands()/2*6;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002537 } else if (NumOps*2 > NumOperands) {
2538 // No resize needed.
2539 if (ReservedSpace >= NumOps) return;
2540 } else if (NumOps == NumOperands) {
2541 if (ReservedSpace == NumOps) return;
2542 } else {
Chris Lattnerf711f8d2005-01-29 01:05:12 +00002543 return;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002544 }
2545
2546 ReservedSpace = NumOps;
2547 Use *NewOps = new Use[NumOps];
2548 Use *OldOps = OperandList;
2549 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
2550 NewOps[i].init(OldOps[i], this);
2551 OldOps[i].set(0);
2552 }
2553 delete [] OldOps;
2554 OperandList = NewOps;
2555}
2556
2557
2558BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
2559 return getSuccessor(idx);
2560}
2561unsigned SwitchInst::getNumSuccessorsV() const {
2562 return getNumSuccessors();
2563}
2564void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
2565 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002566}
Chris Lattnerf22be932004-10-15 23:52:53 +00002567
2568
2569// Define these methods here so vtables don't get emitted into every translation
2570// unit that uses these classes.
2571
2572GetElementPtrInst *GetElementPtrInst::clone() const {
2573 return new GetElementPtrInst(*this);
2574}
2575
2576BinaryOperator *BinaryOperator::clone() const {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00002577 return create(getOpcode(), Ops[0], Ops[1]);
Chris Lattnerf22be932004-10-15 23:52:53 +00002578}
2579
Chris Lattner0b490b02007-08-24 20:48:18 +00002580FCmpInst* FCmpInst::clone() const {
2581 return new FCmpInst(getPredicate(), Ops[0], Ops[1]);
2582}
2583ICmpInst* ICmpInst::clone() const {
2584 return new ICmpInst(getPredicate(), Ops[0], Ops[1]);
Reid Spencerd9436b62006-11-20 01:22:35 +00002585}
2586
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002587MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
2588AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
2589FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
2590LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
2591StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
2592CastInst *TruncInst::clone() const { return new TruncInst(*this); }
2593CastInst *ZExtInst::clone() const { return new ZExtInst(*this); }
2594CastInst *SExtInst::clone() const { return new SExtInst(*this); }
2595CastInst *FPTruncInst::clone() const { return new FPTruncInst(*this); }
2596CastInst *FPExtInst::clone() const { return new FPExtInst(*this); }
2597CastInst *UIToFPInst::clone() const { return new UIToFPInst(*this); }
2598CastInst *SIToFPInst::clone() const { return new SIToFPInst(*this); }
2599CastInst *FPToUIInst::clone() const { return new FPToUIInst(*this); }
2600CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); }
2601CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
2602CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
2603CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
2604CallInst *CallInst::clone() const { return new CallInst(*this); }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002605SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
2606VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
2607
Chris Lattnerbbe0a422006-04-08 01:18:18 +00002608ExtractElementInst *ExtractElementInst::clone() const {
2609 return new ExtractElementInst(*this);
2610}
2611InsertElementInst *InsertElementInst::clone() const {
2612 return new InsertElementInst(*this);
2613}
2614ShuffleVectorInst *ShuffleVectorInst::clone() const {
2615 return new ShuffleVectorInst(*this);
2616}
Chris Lattnerf22be932004-10-15 23:52:53 +00002617PHINode *PHINode::clone() const { return new PHINode(*this); }
2618ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
2619BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
2620SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
2621InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
2622UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
Chris Lattner5e0b9f22004-10-16 18:08:06 +00002623UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}